Java 不要请求 Window.FEATURE_ACTION_BAR 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30923403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:22:57  来源:igfitidea点击:

Do not request Window.FEATURE_ACTION_BAR issue

javaandroidxmlappcompatactivity

提问by End.Game

I'm trying to build my app but without success.. I tried several way but nothing was worked. The exception is:

我正在尝试构建我的应用程序,但没有成功。我尝试了几种方法,但没有任何效果。例外是:

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.

My style.xml is:

我的 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>

and as you can see i have declared

正如你所看到的,我已经宣布

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

This is my manifest.xml

这是我的 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>

and my BaseActivitythat i use to extend other activities

和我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);
    }

}

I don't know why it crashes..The only way to start the application without crash is set the parent on the style.xml in Theme.AppCompat.Light.NoActionBarbut in this way the status bar is transparent and not colored...

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

采纳答案by Bryan Herbst

Using Theme.AppCompat.Lighttells Android that you want the framework to provide an ActionBar for you. However, you are creating your own ActionBar (a Toolbar), so you are giving the framework mixed signals as to where you want the ActionBar to come from.

UsingTheme.AppCompat.Light告诉 Android 您希望框架为您提供 ActionBar。但是,您正在创建自己的 ActionBar (a Toolbar),因此您向框架提供了关于您希望 ActionBar 来自何处的混合信号。

Since you are using a Toolbar, you want Theme.AppCompat.Light.NoActionBar.

由于您使用的是工具栏,因此您需要Theme.AppCompat.Light.NoActionBar.

The next step is to make sure your Toolbar is styled correctly, which seems to be where you are running into issues. To style your Toolbar like an ActionBar using the colors you defined for your theme, you need to provide a theme like so:

下一步是确保您的工具栏样式正确,这似乎是您遇到问题的地方。要使用您为主题定义的颜色将工具栏设置为类似于 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" />

See the "styling" section for the Toolbar widget on this Android Developers blog postfor more information.

有关更多信息,请参阅此 Android 开发人员博客文章中工具栏小部件的“样式”部分。

回答by Heisenberg

In my case, I created a new activity and forgot to define it's app:theme in AndroidManifest.xml:

就我而言,我创建了一个新活动,但忘记在 AndroidManifest.xml 中定义它的 app:theme:

 <activity
        android:name=".HistoryActivity"
        android:label="History"
        android:theme="@style/AppTheme.Primary"></activity>

回答by Alex Jolig

Assuming you got this lines in styles.xml:

假设你有这样的行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>

Add these extra lines after:

在后面添加这些额外的行:

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

Then add this to your activity in manifest:

然后将其添加到清单中的活动中:

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

That was best solution for me after checking tutorials and other solutions

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

回答by Fa Sche

I have solved the problem after changing the value for the "android:theme" for the Activity in the manifest from "@style/AppTheme" to "@style/AppTheme.NoActionBar":

将清单中 Activity 的“android:theme”值从“@style/AppTheme”更改为“@style/AppTheme.NoActionBar”后,我解决了这个问题:

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

回答by Mohit Baghel

<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>