这个 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个回答

6

前往项目的 'style.xml' 文件,将 windowActionBar 设为 false。

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

13
当你的回答与我的问题不符合时,你如何帮助他人? - tyczj
不要使用“Theme.AppCompat.Light”,而是使用“Theme.AppCompat.Light.NoActionBar”。 - Christina

5

如果您在这一行遇到错误:

setSupportActionBar(...);

您需要检查您的活动是否引用了包含工具栏的主题。您的应用程序的AppTheme可能已经包含了一个工具栏,例如

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

如果您正在尝试添加第二个主题,而且想要使用应用程序的AppTheme,则需要在manifest.xml文件中删除活动中的主题。

例如:

<activity
    android:name=".ui.activities.SettingsActivity"
    android:theme="@style/AppTheme1" /> --> remove this theme

5
如果您正在使用Appcompact活动,请在主题中添加以下三行代码。
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowActionBarOverlay">false</item>

5
在您的应用程序主题中的style.xml文件中添加这两行代码:
 <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>

    //Add this two line will fix your problem
    <item name="windowNoTitle">true</item>   <--1(this applies after 22.0.1 support library i think 
    <item name="windowActionBar">true</item> <--2


4

我在xml中添加了工具栏。然后在活动中,我添加了以下语句:

setSupportActionBar(toolbar);

我移除了这个,它对我有用。希望能帮到别人。


如果我不想删除它怎么办?也就是说有一些东西使用了工具栏。 - Sreekanth Karumanaghat
我不确定,但我认为如果删除这个“setSupportActionBar(toolbar);”,工具栏将会保留在那里。 - Harry .Naeem

3
这是我解决问题的方法。在你的AndroidMainfest.xml文件中添加以下代码。
<activity android:name=".YourClass"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
 </activity>

1
太好了!感谢分享。 - Sandeep Yohans

3

简单来说,您可以做以下事情:

if (android.os.Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));

    }

3

非常简单,你只需要选择以下两个步骤中的任意一个即可。

  1. 将您设备的显示模式从暗黑模式更改为正常模式;或者
  2. 如果您正在使用AppCompat,请在您的主题/样式的style.xml中添加以下2行:<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>

2
尝试更改应用程序主题的父级,请在themes.xml中更改以下内容。
<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">

为了

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

以下是在主题定义中禁用操作栏的正确方法:

OR

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

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

    Change this to NoActionBar 

        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->

        </style>

    This one worked for me

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