【问题标题】:Do not request Window.FEATURE_ACTION_BAR issue不要请求 Window.FEATURE_ACTION_BAR 问题
【发布时间】:2015-09-04 13:36:03
【问题描述】:

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

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 上设置父级,但是这样状态栏是透明的并且没有颜色...

【问题讨论】:

    标签: java android xml appcompatactivity


    【解决方案1】:

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

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

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

    有关详细信息,请参阅 this Android Developers blog post 上工具栏小部件的“样式”部分。

    【讨论】:

    • 好吧,看来它正在工作..我认为问题出在工具栏的 app:theme 上。谢谢!
    【解决方案2】:

    假设您在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>
    

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

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

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

    【讨论】:

      【解决方案3】:

      在我的例子中,我创建了一个新活动,却忘记在 AndroidManifest.xml 中定义它的 app:theme:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:
          <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>
          

          【讨论】:

          • 请解释您的解决方案,而不是只提供代码
          • 首先你必须在下面的值/样式中编写这段代码。 然后把这个写到你的 main_activity.. 最后你只需要在你的 manifest.xml 中写入最后的代码:
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-07-27
          • 2015-07-22
          • 2011-11-08
          • 2014-01-22
          • 2013-04-25
          • 2021-10-20
          • 1970-01-01
          相关资源
          最近更新 更多