Discover pit
Recently, when configuring the project theme, the following error was reported:
This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR
Reason 1
Wrong writing method:
<style name=""> <item name="android:windowActionBar">false</item> <item name="android:windowNoTitle">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> </style>
Among them, the theme used by AppTheme is the theme of AppCompat. Since the name of windowActionBar and windowNoTitle under the AppCompat theme does not have Android words before, an error is reported.
Correct writing:
<style name=""> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> </style>
Reason 2
If the theme is set to the Theme with Actionbar and does not match:
<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
Such errors will also occur.
Check out the source code:
When we set the toolbar: ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
Clicking into the source code, you can see that the source code call logic is:
public void setSupportActionBar(@Nullable Toolbar toolbar) { getDelegate().setSupportActionBar(toolbar); }
Following down, the truth is revealed:
public void setSupportActionBar(Toolbar toolbar) { if (!(mOriginalWindowCallback instanceof Activity)) { // Only Activities support custom Action Bars return; } //Here we will check whether there is an actionbar. If there is a direct throwing exception final ActionBar ab = getSupportActionBar(); if (ab instanceof WindowDecorActionBar) { throw new 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."); } // If we reach here then we're setting a new action bar // First clear out the MenuInflater to make sure that it is valid for the new Action Bar mMenuInflater = null; // If we have an action bar currently, destroy it if (ab != null) { (); } if (toolbar != null) { final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, ((Activity) mContext).getTitle(), mAppCompatWindowCallback); mActionBar = tbab; (()); } else { mActionBar = null; // Re-set the original window callback since we may have already set a Toolbar wrapper (mAppCompatWindowCallback); } invalidateOptionsMenu(); }
Mainly here:
//Here we will check whether there is an actionbar. If there is a direct throwing exceptionfinal ActionBar ab = getSupportActionBar(); if (ab instanceof WindowDecorActionBar) { throw new 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."); }
OK, it's over.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.