不要请求Window.FEATURE_ACTION_BAR问题

21

我正在尝试构建我的应用程序,但没有成功... 我尝试了多种方法,但什么都没起作用。异常信息如下:

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

我的style.xml文件如下:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light">

        <!-- theme customizations -->

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>

        <item name="android:textColorPrimary">@color/ldrawer_color</item>
        <item name="actionMenuTextColor">@color/ldrawer_color</item>
        <item name="android:textColorSecondary">@color/ldrawer_color</item>
    </style>

</resources>

正如您所看到的,我已经声明了

<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>

这是我的 manifest.xml

 <application
        android:allowBackup="true"
        tools:replace="android:icon"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

我使用的BaseActivity,它用于扩展其他活动

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public abstract class BaseActivity extends AppCompatActivity {

    public Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(BaseActivity.this.getResources().getString(R.string.app_name));
            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    protected abstract int getLayoutResource();

    protected void setActionBarIcon(int iconRes) {
        toolbar.setNavigationIcon(iconRes);
    }

}

我不知道为什么它会崩溃...唯一能使应用程序启动而不崩溃的方法是在 Theme.AppCompat.Light.NoActionBar 中的 style.xml 上设置父级,但这样状态栏是透明的而不是有色的...

5个回答

49

使用Theme.AppCompat.Light会告诉Android您希望框架为您提供ActionBar。 但是,由于您正在创建自己的ActionBar(一个Toolbar),因此您向框架发出了有关ActionBar来源的混合信号。

由于您正在使用Toolbar,因此您需要使用Theme.AppCompat.Light.NoActionBar

下一步是确保您的Toolbar正确地进行了样式设置,这似乎是您遇到问题的地方。为使您的Toolbar能够像ActionBar一样使用您在主题中定义的颜色进行样式设置,您需要提供以下主题:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

请参考这篇Android开发者博客文章中的“样式”部分,了解有关工具栏小部件的更多信息。


好的,看起来它正在工作...我认为问题出在工具栏的app:theme上。谢谢! - Atlas91

13
假设您在styles.xml中有以下这些行:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

在这些内容后添加以下额外的行:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

然后将以下内容添加到你的manifest中的activity中:

<activity android:name=".activity.YourActivity"
          android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE -->

在查看教程和其他解决方案后,这对我来说是最好的解决方案。


4
在我的情况下,我创建了一个新的活动并忘记在AndroidManifest.xml中定义它的app:theme:
 <activity
        android:name=".HistoryActivity"
        android:label="History"
        android:theme="@style/AppTheme.Primary"></activity>

1

我在清单文件中将Activity的"android:theme"属性的值从"@style/AppTheme"改为"@style/AppTheme.NoActionBar"后,解决了这个问题:

<activity android:name=".MyXXXActivity"
            ...                android:theme="@style/AppTheme.NoActionBar" />

0
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

 <android.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.NoActionBar">

 </android.appcompat.widget.Toolbar>
 <activity android:name=".activity.activity_name"
           android:theme="@style/AppTheme.NoActionBar">
 </activity>

请解释您的解决方案,而不仅仅是提供代码。 - D. Lawrence
首先,您必须编写以下值/样式中的代码。<style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> 然后将其写入您的main_activity中..<android.appcompat.widget.Toolbar android:theme="@style/AppTheme.NoActionBar"></android.appcompat.widget.Toolbar>最后,只需将最后一个代码写入您的manifest.xml即可: - Mohit Baghel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接