本文翻译自:This Activity already has an action bar supplied by the window decor
Trying to move over my stuff to use Toolbar
instead of action bar but I keep getting an error saying 尝试移动我的东西以使用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)
so then I added in my style for my activity to have no actionbar 因此,我添加了自己的风格以使我的活动没有操作栏
<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
</style>
and the theme is applies to activties in my manifest 这个主题适用于我的清单中的活动
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize|stateHidden"
android:theme="@style/AppCompatTheme" android:screenOrientation="portrait"/>
MainActivity extends GooglePlayServiceActivity so I also set the theme there too MainActivity扩展了GooglePlayServiceActivity,所以我也在那里设置了主题
<activity
android:name=".GooglePlayServicesActivity"
android:label="@string/title_activity_google_play_services"
android:theme="@style/AppCompatTheme">
but I still get the error. 但是我仍然得到错误。 I also do not request window feature anywhere. 我也不会在任何地方请求窗口功能。 any ideas why I still get this? 任何想法为什么我仍然得到这个?
#1楼
参考:https://stackoom.com/question/1nFmE/此活动已具有窗口装饰提供的操作栏
#2楼
I think you're developing for Android Lollipop, but anyway include this line: 我认为您正在开发Android Lollipop,但无论如何都包括以下内容:
<item name="windowActionBar">false</item>
to your theme declaration inside of your app/src/main/res/values/styles.xml
. 到app/src/main/res/values/styles.xml
内的主题声明。
Also, if you're using AppCompatActivity
support library of version 22.1 or greater, add this line: 另外,如果您使用的是22.1或更高版本的AppCompatActivity
支持库,请添加以下行:
<item name="windowNoTitle">true</item>
Your theme declaration may look like this after all these additions: 在完成所有这些添加之后,您的主题声明可能看起来像这样:
<!-- 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>
#3楼
Another easy way is to make your theme a child of Theme.AppCompat.Light.NoActionBar
like so: 另一个简单的方法是使您的主题成为Theme.AppCompat.Light.NoActionBar
的子级, Theme.AppCompat.Light.NoActionBar
所示:
<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
#4楼
Go to the 'style.xml' of your project and make windowActionBar to false 转到项目的“ style.xml”,并将windowActionBar设置为false
<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
</style>
#5楼
I also faced same problem. 我也面临同样的问题。 But I used: 但是我用过:
getSupportActionBar().hide();
before 之前
setContentView(R.layout.activity_main);
Now it is working. 现在正在工作。
And we can try other option in Style.xml , 我们可以在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>
#6楼
Add this in your values/styles.xml 将此添加到您的values / styles.xml中
<style name="YourCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="AppBaseTheme" parent="YourCustomTheme">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
And add the following code in your values-v11/styles.xml and values-v14/styles.xml 并在values-v11 / styles.xml和values-v14 / styles.xml中添加以下代码
<style name="AppBaseTheme" parent="YourCustomTheme">
</style>
Thats it. 而已。 It will work. 它会工作。