java.lang.IllegalStateException: This Activity already has an action bar supplied by the window deco

来源:互联网 发布:php require 编辑:程序博客网 时间:2024/05/09 12:18

最近在使用toolbar的过程中遇到了一个error,提示是这样的:

     Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

还有一种情况是这样的:

 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

其实这主要还是我用android.support.v7.widget包里面的toolbar导致的,上面也说得很清楚in your theme to use a Toolbar instead.这接下来就非常简单了,我们直接在主题里面设置下就可以了,如下:

  <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style>//自己定义的,把actionBar挂掉就可以了,以及TItle    <style name="AppTheme.NoActionBar">        <item name="windowActionBar">false</item>        <item name="windowNoTitle">true</item>    </style>

tips:


Please mark that, even if api levels higher than 10 already have actionbar you should still use AppCompat styles. If you don’t, you will have this error on launch of Acitvity on devices with android 3.0 and higher:

0 0