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

相關文章
相關標籤/搜索