这个 Activity 已经由窗口装饰提供了一个操作栏

595
尝试将我的东西移动到使用工具栏而不是操作栏,但我一直收到一个错误消息。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tyczj.weddingalbum/com.xxx.xxx.MainActivity}: 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.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     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.
            at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:165)
            at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
            at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:113)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

然后我为我的活动添加了样式,使其没有操作栏。

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>
</style>

主题适用于我的清单中的活动

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize|stateHidden"
        android:theme="@style/AppCompatTheme" android:screenOrientation="portrait"/>

MainActivity扩展了GooglePlayServiceActivity,因此我也在那里设置了主题。

<activity
       android:name=".GooglePlayServicesActivity"
       android:label="@string/title_activity_google_play_services"
       android:theme="@style/AppCompatTheme">

但我仍然收到错误提示。我也没有在任何地方请求窗口功能。有任何想法为什么我仍然会出现这种情况吗?

27个回答

957

我认为你正在为Android Lollipop开发,但无论如何,请包含这行代码:

<item name="windowActionBar">false</item> 

将以下内容添加到app/src/main/res/values/styles.xml文件中的主题声明中。

另外,如果您正在使用22.1或更高版本的AppCompatActivity支持库,请添加此行:

<item name="windowNoTitle">true</item>

在所有这些添加之后,您的主题声明可能如下所示:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

19
windowActionBarandroid:windowActionBar不是同一个东西吗? - tyczj
68
这是因为 AppCompat 的东西查看应用程序命名空间属性而不是 Android 命名空间属性。这使得即使在最初没有该属性的平台上也可以指定该属性。这就是它如何允许像 colorPrimary 这样的后移植。 - yarian
5
当我在样式中添加那行代码时,应用程序崩溃了 :( - rkmax
9
还需要添加 <item name="windowNoTitle">true</item>。如果您在22.1支持库之后使用AppCompatActivity,请参考https://dev59.com/kF0a5IYBdhLWcg3w88g0#29853299。 - Nick
8
不要忘记在清单文件的应用程序标签中添加 android:theme="@style/AppTheme"。 - live-love
显示剩余5条评论

221

另一种简单的方法是将您的主题作为Theme.AppCompat.Light.NoActionBar的子主题,如下所示:

<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
     ...
</style>

我也尝试了这种方法,但仍然出现错误。不过这可能是在我将主题设置为我的“GooglePlayServiceActivity”之前,我记不清了。 - tyczj
2
是的,这就是我一直在使用的。只需确保活动在清单中使用此主题。 - MrEngineer13
6
虽然 @Nadir Belhaj 的解决方案也是正确的,但我认为这个解决方案更加优雅。 - voghDev
我认为这是更好的答案,使用内联XML属性(windowActionBar=false)没有起作用 - 我得到了与@tyczj相同的错误。 - Booger
在这种情况下,您不能使用Theme.AppCompat.Light.DarkActionBar,它实际上是一个有用的父级。 - f470071

153

AndroidManifest.xmlactivity标签中添加单行 android:theme="@style/AppTheme.NoActionBar",然后你就完成了。

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

样式.xml

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

57

要将工具栏用作操作栏,首先需要禁用由装饰提供的操作栏。

最简单的方法是使您的主题扩展自

Theme.AppCompat.NoActionBar

(或其浅色变体)。

其次,创建一个工具栏实例,通常通过您的布局 XML:

<android.support.v7.widget.Toolbar
    android:id=”@+id/my_awesome_toolbar”
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”?attr/actionBarSize”
    android:background=”?attr/colorPrimary” />

然后在你的Activity或Fragment中,将Toolbar设置为作为你的操作栏:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blah);

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
}

这段代码对我有效。


1
谢谢!我忘记在XML中添加<android.support.v7.widget.Toolbar>了。 - CoolMind
1
我一直无法弄清楚为什么我的工具栏颜色只是空白的。似乎找不到如何将其更改为绿色的方法。感觉很傻,因为我所要做的就是在布局文件中<android.support.v7.widget.Toolbar />内添加android:background=" ",问题就解决了!谢谢。 - Andrew Irwin

24
如果您想将某些活动与操作栏组合在一起而其他一些不需要,则应使用具有启用操作栏的基本主题,然后创建一个子主题,用于在不需要操作栏的活动上使用。
例如,您可以使用这样的子样式:
             <style name="AppTheme.NoActionBar">
               <item name="windowActionBar">false</item>
               <item name="windowNoTitle">true</item>
            </style>

虽然基础主题会扩展说

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

然后在AndroidManifest文件中的activity标签内使用非actionbar主题。

   <activity
        android:name="com.example.NonActionBarActivity"
        android:theme="@style/AppTheme.NoActionBar"

你必须将其应用于不需要Action Bar的每个单独的活动,因此如果你的项目需要较少的Action Bar活动,则最好在基础主题级别上应用此方法。


感谢!我如何使一个自定义工具栏使用我在活动中动态设置的2种不同样式,根据应用程序中所需的模式? - Skj

14

我也遇到了同样的问题。但是我使用了:

getSupportActionBar().hide();

setContentView(R.layout.activity_main);

之前。

现在它可以正常工作了。

我们还可以尝试在Style.xml中尝试其他选项。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

9

将以下内容添加到您的 values/styles.xml 文件中:

<style name="YourCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

<style name="AppTheme" parent="AppBaseTheme">

</style>

请在您的values-v11/styles.xml和values-v14/styles.xml文件中添加以下代码:

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

就是这样,它会起作用。


7

在你的应用程序标签中使用此代码,或者在你的Activity标签中使用。

android:theme="@style/AppTheme.NoActionBar"


7

您需要更改

  <activity
        android:name=".YOUR ACTIVITY"
        android:theme="@style/AppTheme.NoActionBar" />
</application>`

这些在清单文件中的行。对我来说它完美地工作。


7

或者简单地更改@Style/AppTheme

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

这对我有效!


如果你想使用工具栏而不是操作栏,这个方法非常有效。有没有想过为什么不能同时使用两者? - Prajwal Waingankar

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