This Activity already has an action bar supplied by the window decor

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

错误信息:

  • 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进行替换时出错

解决思路:想办法去掉ActionBar

解决方式

  • 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版本号下不能识别,删除保留其它就可以。

转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992

原文地址:https://www.cnblogs.com/claireyuancy/p/7050229.html