Android 设置theme中的坑

转载自:http://blog.csdn.net/zzz_zzz_z/article/details/51737876

报错如下:

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

其中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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

原因二:

如果主题设置成有Actionbar的Theme并且没有配:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
  • 1
  • 2

也会出这样的错误

看下源码:

在我们设置toolbar时候:

((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

可以看到源码调用逻辑是:

public void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
    }
  • 1
  • 2
  • 3

在往下追一步,出真相了:

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();
        }

        if (toolbar != null) {
            final ToolbarActionBar tbab = new ToolbarActionBar(toolbar,
                    ((Activity) mContext).getTitle(), mAppCompatWindowCallback);
            mActionBar = tbab;
            mWindow.setCallback(tbab.getWrappedWindowCallback());
        } else {
            mActionBar = null;
            // Re-set the original window callback since we may have already set a Toolbar wrapper
            mWindow.setCallback(mAppCompatWindowCallback);
        }

        invalidateOptionsMenu();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

主要在这里:

//这里会去判有没有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.");
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

y   

原因三:继承的主题有问题

   之前的是:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

 因为这个主题是带 acitionBar的所以有添加一个actionBar故会报错

更改之后:

< style name= "AppTheme" parent= " Theme.AppCompat.Light.NoActionBar " >
<!-- Customize your theme here. -->
< item name= "colorPrimary" >@color/colorPrimary</ item >
< item name= "colorPrimaryDark" >@color/colorPrimaryDark</ item >
< item name= "colorAccent" >@color/colorAccent</ item >
</ style >


程序正常运行!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值