尝试使用AppCompat v21实现工具栏

时间:2014-11-29 12:29:49

标签: android toolbar android-appcompat

我尝试按照本指南http://antonioleiva.com/material-design-everywhere/使用AppCompat v21实现工具栏但是我无法在KitKat或Lollipop上启动并获得相同的错误消息(如下所示)。我完全相信我已经正确设置了主题,但我在这一点上陷入困​​境并且已经持续了一天多。我没有成功搜索解决方案,所以现在我要求一些帮助。谢谢!

> 11-29 06:08:46.892  29771-29771/com.chaoticearth.chaoticearth E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ce.ce, PID: 29771
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ce.ce/com.ce.ce.DrawerActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
            at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
            at com.ce.ce.BaseActivity.onCreate(BaseActivity.java:16)
            at com.ce.ce.DrawerActivity.onCreate(DrawerActivity.java:92)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)

这是我的/res/values/themes.xml

<resources>

<style name="CETheme" parent="AppTheme" ></style>
<style name="AppTheme" parent="Theme.AppCompat">
    <item name="android:colorPrimaryDark">@color/MidnightBlue</item>
    <item name="android:colorAccent">@color/LightYellow</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>

</style>

这是我的/res/values-v21/themes.xml

<?xml version="1.0" encoding="utf-8"?>

    

    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>

    <item name="android:colorPrimary">@color/ChaosBlue</item>
    <item name="android:colorPrimaryDark">@color/MidnightBlue</item>
    <item name="android:colorAccent">@color/LightYellow</item>
    <item name="android:statusBarColor">@color/ChaosBlue</item>

</style>

以下是我的DrawerActivity.java中引发错误的片段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setActionBarIcon(R.drawable.ic_drawer);

正在调用BaseActivity.java的片段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResource());
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

最后这是AndroidManifest.xml的片段

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@style/CETheme"
    android:name="android.support.multidex.MultiDexApplication">

2 个答案:

答案 0 :(得分:3)

我终于发现了原因,在我的AndroidManifest中,我为DrawerActivity设置了一个主题,这导致了错误。

答案 1 :(得分:1)

尝试使用 colorPrimaryDark 替换 android:colorPrimaryDark ,并在res / values /中使用 colorAccent 替换 android:colorAccent 的themes.xml。