1,问题
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
当在代码中添加setSupportActionBar(toolbar)时报错
Unable to start activity ComponentInfo: 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.
2, 原因:
可能是因为在application节点中引用了 @style/AppTheme样式,而这个样式自带actionbar
所以我们在activit里面 setSupporActivity(toolbar)就会报这个错误。
3,解决方法:
方法一、
去掉代码中的setSupportActionBar(toolbar);
方法二、
找到src/main/res/values/styles.xml文件,打开内容如下:
<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>
将其中的Theme.AppCompat.Light.DarkActionBar 改为:
Theme.AppCompat.Light.NoActionBar即可