ActionBar的那些坑

来源:互联网 发布:压缩弹簧计算软件 编辑:程序博客网 时间:2024/05/16 11:40

问题异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.toobar/com.example.administrator.toobar.MainActivity}: 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.


何种情况:学习使用v7下的toobar时,在xml中添加

<android.support.v7.widget.Toolbar ..... /> 
在activity中setSupportActionBar(toolBar)报错。
原因是你已经有了一个actionbar,不能再添加了。
注意:将androidmanifest.xml 中<application >标签下的theme修改一下成没有actionbar的样式。
原来是这样的,系统默认
<!-- Base application theme. --><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>
修改之后
<!-- Base application theme. --><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>
这时候运行就没有问题了。



原创粉丝点击