没有合适的资源?快使用搜索试试~ 我知道了~
首页Android设置theme中可能遇到的坑
发现坑 最近在配置项目主题的时候报了如下错误: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR 原因一 错误写法: <style name=AppTheme.NoActionBar> <item name=android:windowActionBar>false</item> <item name=android:windowNoTitle>true</item> <ite
资源详情
资源评论
资源推荐

Android设置设置theme中可能遇到的坑中可能遇到的坑
发现坑发现坑
最近在配置项目主题的时候报了如下错误:
This Activity already has an action bar supplied by the window decor. Do not request
Window.FEATURE_SUPPORT_ACTION_BAR
原因一原因一
错误写法:
<style name="AppTheme.NoActionBar">
<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>
其中AppTheme使用的主题是AppCompat的主题,由于AppCompat主题下的windowActionBar和windowNoTitle的命名方式前
都没有android字样,所以报错。
正确写法:正确写法:
<style name="AppTheme.NoActionBar">
<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>
原因二原因二
如果主题设置成有Actionbar的Theme并且没有配:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
也会出这样的错误。
看下源码:看下源码:
在我们设置toolbar时候: ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);点进源码可以看到源码调用逻辑是:
public void setSupportActionBar(@Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
在往下追一步,出真相了:
public void setSupportActionBar(Toolbar toolbar) {
if (!(mOriginalWindowCallback instanceof Activity)) {
// Only Activities support custom Action Bars
return;
}
//这里会去判有没有actionbar存在,如果有直接抛异常
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) {
ab.onDestroy();














安全验证
文档复制为VIP权益,开通VIP直接复制

评论0