简体   繁体   English

尝试使用AppCompat v21实施工具栏

[英]Trying to implement Toolbar with AppCompat v21

I'm trying to implement the Toolbar using AppCompat v21 following this guide, http://antonioleiva.com/material-design-everywhere/ however I am unable to launch on KitKat or Lollipop and get the same error message (below). 我正在尝试按照本指南( http://antonioleiva.com/material-design-everywhere/)使用AppCompat v21实施工具栏,但是我无法在KitKat或Lollipop上启动并得到相同的错误消息(如下)。 I fully believe I have the theme set up correctly but I am stuck at this point and have been for over a day. 我完全相信我已经正确设置了主题,但此时此刻,我已经停留了整整一天。 I've searched unsuccessfully for the solution so now I'm asking for some help. 我没有成功找到解决方案,因此现在寻求帮助。 Thanks! 谢谢!

> 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)

Here is my /res/values/themes.xml 这是我的/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>

Here is my /res/values-v21/themes.xml 这是我的/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>

Here is a snippet from my DrawerActivity.java where the error is being thrown: 这是我的DrawerActivity.java中的一个片段,其中抛出了错误:

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

And a snippet from the BaseActivity.java that is being called out: 还有一个从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);
    }
}

And finally here is a snippet from my AndroidManifest.xml 最后是我的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">

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

尝试更换机器人:colorPrimaryDarkcolorPrimaryDarkandroid:colorAccentcolorAccent在RES /价值/的themes.xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM