This Activity already has an action bar supplied b

问题描述:继承自AppCompatActivity,使用Toolbar替代ActionBar的时候,出现错误 java

错误信息: android

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

问题缘由描述:因为Activity已存在ActionBar,因此使用Toolbar进行替换时出错 spa

解决思路:想办法去掉ActionBar code

解决方案 xml

  • 1.使用Theme去掉ActionBar。使用Theme.AppCompat.Light.NoActionBar或者是Theme.AppCompat.NoActionBar主题,便可去掉ActionBar,便可解决此问题。 
    代码以下
<resources>
    <style name="AppTheme" parent="AppTheme.Base">
    </style>
    <style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/accent_material_light</item>
        <item name="colorPrimaryDark">@color/accent_material_light</item>
        <item name="android:windowBackground">@color/dark</item>
    </style>
</resources>
  • 2.若不能使用以上方案,则设置Theme的属性解决此问题。

在项目中的全部values-xx文件夹中的styles.xml中添加下面代码,从而去掉ActionBar 继承

<item name="windowActionBar">false</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowNoTitle">true</item>

有一个细节须要注意,由于这几个属性对版本的要求不一样,因此若是某个属性在你的App版本下不能识别,删除保留其余便可。 it

相关文章
相关标签/搜索