【问题标题】:This Activity already has an action bar supplied by the window decor这个 Activity 已经有一个由窗口装饰提供的操作栏
【发布时间】:2016-05-16 23:07:46
【问题描述】:

试图移动我的东西以使用 Toolbar 而不是操作栏,但我一直收到错误提示

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

但我仍然收到错误消息。我也不要求任何地方的窗口功能。任何想法为什么我仍然得到这个?

【问题讨论】:

    标签: android android-actionbar material-design android-5.0-lollipop android-toolbar


    【解决方案1】:

    尝试更改应用程序主题的父级

    在themes.xml下面更改

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

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

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

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

    【讨论】:

      【解决方案2】:

      这很简单,你只需要两步中的任何一步..

      1. 将您的设备显示模式从深色模式更改为正常模式或。
      2. 如果您使用的是 AppCompat 在 style.xml 中,则将这两行添加到您的主题/样式中..&lt;item name="windowActionBar"&gt;false&lt;/item&gt; &lt;item name="windowNoTitle"&gt;true&lt;/item&gt;

      【讨论】:

        【解决方案3】:

        也可能是因为您的样式没有指定父级:

        <style name="DarkModeTheme" >
            <item name="searchBarBgColor">#D6D6D6</item>
        </style>
        

        删除它,它应该可以解决问题。

        【讨论】:

          【解决方案4】:

          由于内部的自定义属性,我收到此错误,删除它修复了问题。

              <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          ..
                  <item name="searchBarBgColor">#383838</item>  --> remove this line
              </style>
          

          【讨论】:

            【解决方案5】:

            或者只需更改@Style/AppTheme

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

            这对我有用!

            【讨论】:

            • 如果您想使用工具栏而不是简单地不使用操作栏,这将非常有效。知道为什么我们不能同时使用两者吗?
            【解决方案6】:
             <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
            

            【讨论】:

              【解决方案7】:

              在清单中的应用程序标签或活动标签内使用它。

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

              【讨论】:

                【解决方案8】:

                如果你在这一行得到错误:

                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
                

                【讨论】:

                  【解决方案9】:

                  只需将parent="Theme.AppCompat.NoActionBar" 放入您的 style.xml 文件中

                  点赞-&lt;style parent="Theme.AppCompat.NoActionBar" name="AppTheme.NoActionBar"&gt;

                  【讨论】:

                    【解决方案10】:

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

                    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>
                    

                    【讨论】:

                      【解决方案11】:

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

                      setSupportActionBar(toolbar);
                      

                      删除它对我有用。我希望它可以帮助某人。

                      【讨论】:

                      • 如果我不想删除它怎么办?那就是使用工具栏的东西。
                      • 我不确定,但我认为删除这个“setSupportActionBar(toolbar);”工具栏将保留在那里。
                      【解决方案12】:

                      如果您想将一些活动与操作栏结合起来,而其他活动则不,您应该使用启用了操作栏的基本主题,然后创建一个子主题,您将在不需要操作栏的活动上使用它

                      例如,您可以使用这样的子样式

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

                      虽然基础主题扩展了说

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

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

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

                      您必须将此应用于不需要操作栏的每个单独的活动,因此如果您的项目需要的操作栏活动少于非活动,那么最好将其应用于基本主题级别

                      【讨论】:

                        【解决方案13】:

                        我通过删除这一行来解决它:

                        android:theme="@style/Theme.MyCompatTheme"

                        来自Manifest file中的活动属性

                        【讨论】:

                          【解决方案14】:

                          如果您使用的是Appcompact Activity,请在您的主题中使用这三行代码。

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

                          【讨论】:

                            【解决方案15】:

                            你需要改变

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

                            清单中的这些行。它非常适合我。

                            【讨论】:

                              【解决方案16】:

                              这就是我解决问题的方法。在您的AndroidMainfest.xml中添加以下代码

                              <activity android:name=".YourClass"
                                          android:theme="@style/Theme.AppCompat.Light.NoActionBar">
                               </activity>
                              

                              【讨论】:

                              • 完美!感谢分享。
                              【解决方案17】:

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

                              最简单的方法是让你的主题扩展自

                              Theme.AppCompat.NoActionBar

                              (或其轻型变体)。

                              其次,创建一个 Toolbar 实例,通常通过您的布局 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 设置为您的 Action Bar:

                              @Override
                              public void onCreate(Bundle savedInstanceState) {
                                  super.onCreate(savedInstanceState);
                                  setContentView(R.layout.blah);
                              
                                  Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
                                  setSupportActionBar(toolbar);
                              }
                              

                              这段代码对我有用。

                              【讨论】:

                              • 谢谢!我忘记在 XML 中添加 &lt;android.support.v7.widget.Toolbar
                              • 我不明白为什么我的工具栏颜色只是空白。似乎无法弄清楚如何将其更改为绿色。我觉得很傻,因为我所要做的就是将 android:background=" " 放在布局文件中的 中,然后就做到了! .谢谢
                              【解决方案18】:

                              AndroidManifest 中添加单行android:theme="@style/AppTheme.NoActionBar"activity,您就完成了。


                              AndroidManifest.xml

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

                              styles.xml

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

                              【讨论】:

                                【解决方案19】:

                                简单地说,你可以做到以下几点:-

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

                                【讨论】:

                                  【解决方案20】:

                                  我认为您正在为 Android Lollipop 进行开发,但无论如何都要包含这一行:

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

                                  app/src/main/res/values/styles.xml 中的主题声明。

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

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

                                  【讨论】:

                                  • windowActionBarandroid:windowActionBar 不同吗?
                                  • 是的,但是 AppCompat 的东西着眼于 app 命名空间属性而不是 android 属性。这使得即使在最初没有该属性的平台中也可以指定该属性。这就是它允许向后移植诸如 colorPrimary 之类的东西的方式。
                                  • 当我在我的样式中添加该行时,应用程序崩溃:(
                                  • 另外添加 true 如果您在 22.1 支持库之后使用 AppCompatActivity stackoverflow.com/a/29853299/1402525
                                  • 别忘了在 manifest 文件的应用标签中添加:android:theme="@style/AppTheme"。
                                  【解决方案21】:

                                  在 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
                                  

                                  【讨论】:

                                    【解决方案22】:

                                    在你的 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>
                                    

                                    就是这样。它会起作用的。

                                    【讨论】:

                                      【解决方案23】:

                                      转到项目的“style.xml”并将 windowActionBar 设置为 false

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

                                      【讨论】:

                                      • 当您对我的问题的回答不正确时,您如何帮助他人
                                      • 不要使用“Theme.AppCompat.Light”,而是使用“Theme.AppCompat.Light.NoActionBar”
                                      【解决方案24】:

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

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

                                      【讨论】:

                                      • 我也试过这个方法,但还是报错。不过那可能是在我将主题设置为我的GooglePlayServiceActivity 之前,我不记得了
                                      • 是的,这就是我一直在使用的。只需确保活动在清单中使用此主题
                                      • 我发现这个解决方案比@Nadir Belhaj 的解决方案更优雅,尽管它们都是正确的。
                                      • 我认为这是更好的答案,使用内联 XML 属性 (windowActionBar=false) 不起作用 - 我遇到与 @tyczj 相同的错误
                                      • 在这种情况下你不能使用 Theme.AppCompat.Light.DarkActionBar 这实际上是一个有用的父级。
                                      猜你喜欢
                                      • 2016-04-22
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 2015-03-03
                                      相关资源
                                      最近更新 更多