安卓爬坑(二)——Toolbar使用事项

本文详细解析了在Android应用中遇到的常见异常:“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.” 介绍了该异常发生的原因,即在使用默认主题或带默认ActionBar主题的activity中调用setSupportActionBar(toolbar),并提供了在style.xml和Manifest.xml中修改theme的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



转自https://www.cnblogs.com/hh9601/p/6404728.html

异常信息:

1
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);
而当前使用的主题为默认,或者带默认ActionBar的主题

当在activity中调用了setSupportActionBar(toolbar);

同时,AndroidManifest.xml对应的Activity标签的android:theme

<android:theme="@style/AppTheme" >

且,style资源文件中的parent为parent="Theme.AppCompat.Light.DarkActionBar

就会报这个异常。

解决方法:

style.xml中加入:

Manifest.xml中,修改theme

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值