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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章