ActionBar重复设置引起错误

来源:互联网 发布:狙击模拟软件 汉化版 编辑:程序博客网 时间:2024/06/06 01:20

错误日志

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.                                                                                 

代码截图

日志分析

该错误由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.
    即在style配置文件中加入:
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

  • 将style配置文件中parent改为parent="Theme.AppCompat.Light.NoActionBar"

  • 将AndroidManifest.xml对应application标签中android:theme="@style/AppTheme"改为android:theme="@android:style/Theme.NoTitleBar"

知识延伸

DecorView是一顶级View,内部包含一竖直方向LinearLayout。该LinearLayout分上下两部分,分titlebar和contentParent两子元素。contentParent的id为content,而我们自定义Activity布局就是contentParent里面一子元素。View层所有事件都先经DecorView后才传递给我们的View。