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

轉載請標明出處:http://blog.csdn.net/donkor_/article/details/77574511

前言
今天在使用ToolBar的時候,gradle編譯更新,彈出瞭如下的錯誤

▲ 異常log :

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

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.

▲ 問題原因 :
在activity中調用了setSupportActionBar(toolbar);

同時,AndroidManifest.xml 對應的Application標籤的android:theme爲
android:theme="@style/AppTheme"

並且,style資源文件中的parent爲parent="Theme.AppCompat.Light.DarkActionBar

就會報這個異常,導致的應用程序崩潰

▲ 問題分析 :

Using Theme.AppCompat.Light tells Android that you want the framework to provide an ActionBar for you. However, you are creating your own ActionBar (a Toolbar), so you are giving the framework mixed signals as to where you want the ActionBar to come from.

翻譯過來大概就是

你使用了Theme.AppCompat.Light 提供的ActionBar,但是,你又自己創建了一個ActionBar(ToolBar),你應該告訴android studio到底要使用的是哪一個

▲ 解決方案
知道了問題的原因,解決起來就方便多了。
在style.xml中加入自定義的主題,把ActionBar關掉:

在Manifest.xml中,修改主題theme

解決~ 完美 ヾ(◍°∇°◍)ノ゙


About me
Email :[email protected]
Android開發交流QQ羣 : 537891203
Android開發交流羣

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