Android设置theme中可能遇到的坑
作者:stone_zhu 时间:2021-08-05 06:20:26
发现坑
最近在配置项目主题的时候报了如下错误:
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();
}
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();
}
主要在这里:
//这里会去判有没有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.");
}
好了,结束。
来源:https://www.jianshu.com/p/f77ef2c90417
标签:android,theme,坑
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#关闭指定名字进程的方法
2021-11-23 23:59:42
使用Android Studio创建OpenCV4.1.0 项目的步骤
2022-05-03 23:16:42
![](https://img.aspxhome.com/file/2023/0/116030_0s.png)
C语言单向链表的表示与实现实例详解
2022-01-24 21:38:33
![](https://img.aspxhome.com/file/2023/4/120804_0s.png)
Android View与Compose互相调用实例探究
2021-06-11 09:07:29
![](https://img.aspxhome.com/file/2023/3/96763_0s.png)
C#中文随机数实现方法
2023-12-07 20:56:30
图文详解OkHttp的超时时间
2022-05-14 13:50:23
![](https://img.aspxhome.com/file/2023/6/71636_0s.png)
Java Socket编程实例(一)- TCP基本使用
2023-11-11 08:42:50
Java Durid进行JDBC连接详解
2022-09-25 02:35:12
datatables 带查询条件java服务端分页处理实例
2023-12-24 08:48:16
React Native与Android 原生通信的方法
2021-08-11 19:02:56
C#中TreeView节点的自定义绘制方法
2023-04-01 10:37:19
ResultSet如何动态获取列名和值
2022-01-16 15:54:01
如何调用chatGPT实现代码机器人
2023-06-05 02:09:33
![](https://img.aspxhome.com/file/2023/0/104960_0s.jpg)
Java实现登录和注册案例
2022-02-27 04:57:46
![](https://img.aspxhome.com/file/2023/9/84779_0s.jpg)
Kotlin空安全空类型浅谈
2022-06-18 22:48:49
![](https://img.aspxhome.com/file/2023/6/123256_0s.png)
Java对象类型的判断详解
2023-07-26 09:55:07
C#实现通过winmm.dll控制声音播放的方法
2022-12-02 06:35:17
java生成图片验证码功能
2023-06-27 00:31:55
![](https://img.aspxhome.com/file/2023/4/60994_0s.png)
Unity 使用tiledmap解析地图的详细过程
2023-06-02 18:40:57
![](https://img.aspxhome.com/file/2023/4/96314_0s.jpg)
详解用RxJava实现事件总线(Event Bus)
2022-02-13 16:43:18
![](https://img.aspxhome.com/file/2023/6/109156_0s.png)