setSupportActionBar()返回IllegalStateException

时间:2016-06-21 16:39:02

标签: android toolbar

我试图在活动中设置工具栏。在我使用setSupportActionBar()后,我有IllegalStateException堆栈跟踪

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.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:126)
at com.fonum.screen.main.MainActivity.showActionBar(MainActivity.java:135)

我搜索了解决方案,找到了this并且答案更少。我尝试使用像他们说的主题,但它仍然给我这个错误

这是我的style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="colorControlActivated">@color/switch_color</item>
    <item name="android:colorForeground">@color/switch_color_transparent</item>
</style>
<style name="action_bar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
</style>

这是我的机器人清单:

<application
    android:allowBackup="true"
    android:fullBackupContent="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/AppTheme"
    tools:replace="android:allowBackup">

这是我的main.xml:

<RelativeLayout
xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:background="@color/white">

<android.support.v7.widget.Toolbar
    a:id="@+id/toolbar"
    a:layout_width="match_parent"
    a:layout_height="wrap_content"
    a:elevation="4dp"
    a:minHeight="@dimen/action_bar_height"
    a:background="@color/primary"
    app:theme="@style/action_bar"/>

<RelativeLayout
    a:id="@+id/container"
    a:layout_width="match_parent"
    a:layout_height="match_parent"
    a:layout_below="@id/toolbar"/>

知道怎么解决吗?

0 个答案:

没有答案