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

来源:互联网 发布:伟星ppr水管颜色 知乎 编辑:程序博客网 时间:2024/05/17 21:55

在项目中使用了toolbar,并在activity里面设置了:setSupportActionBar(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.

分析:
这里是因为AndroidManifest.xml的application节点中引用了 @style/AppTheme样式,这个样式自带actionbar

解决方法:
在styles.xml中自定义一个style:

    <!-- 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>    //自定义的    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">        <item name="android:windowActionBar">false</item>        <item name="windowActionBar">false</item>        <item name="windowNoTitle">true</item>    </style>
阅读全文
0 0