这里写代码片
我们在开发中,对Toolbar已经非常熟悉了,这个东西已经取代了ActionBar了,我们在以往的开发中使用Toolbar的时候,否会遇到一个错误,本人老是犯这个错误,所以写个笔记,记录一下,下面是报的错误:
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已存在ActionBar,所以使用Toolbar进行替换时出错
解决思路:想办法去掉ActionBar
解决方案:
1.使用Theme去掉ActionBar。使用Theme.AppCompat.Light.NoActionBar或者是Theme.AppCompat.NoActionBar主题,即可去掉ActionBar,即可解决此问题。
代码如下:
<style name="AppTheme.NoActionBar">
<!-- Both of these are needed -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
这样一来这个问题就解决了~
有问题,请大家随时交流~工作顺利~