Android 这个 Activity 已经有一个由窗口装饰提供的操作栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26515058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
This Activity already has an action bar supplied by the window decor
提问by tyczj
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.access0(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.access0(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?
但我仍然收到错误。我也不会在任何地方请求窗口功能。任何想法为什么我仍然得到这个?
回答by Nadir Belhaj
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:
此外,如果您使用的AppCompatActivity
是 22.1 或更高版本的支持库,请添加以下行:
<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>
回答by MrEngineer13
Another easy way is to make your theme a child of Theme.AppCompat.Light.NoActionBar
like so:
另一种简单的方法是让你的主题Theme.AppCompat.Light.NoActionBar
像这样:
<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
回答by Volodymyr Kulyk
Add single line android:theme="@style/AppTheme.NoActionBar"
to activity
in AndroidManifest
and you've done.
单行添加android:theme="@style/AppTheme.NoActionBar"
到activity
在AndroidManifest
和你做。
AndroidManifest.xml:
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>
回答by V_J
To use Toolbar as an Action Bar, first disable the decor-provided Action Bar.
要将工具栏用作操作栏,首先禁用装饰提供的操作栏。
The easiest way is to have your theme extend from
最简单的方法是让您的主题从
Theme.AppCompat.NoActionBar
Theme.AppCompat.NoActionBar
(or its light variant).
(或其轻型变体)。
Second, create a Toolbar instance, usually via your layout XML:
其次,通常通过您的布局 XML 创建一个 Toolbar 实例:
<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” />
Then in your Activity or Fragment, set the Toolbar to act as your 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);
}
This code worked for me.
这段代码对我有用。
回答by Nasz Njoka Sr.
If you want to combine some activities with actionbar and others not, you should use the base theme have actionbar enabled and then create a sub theme that you gonna use it on activities that don't require actionbar
如果您想将某些活动与操作栏相结合,而其他活动则不要,您应该使用启用了操作栏的基本主题,然后创建一个子主题,您将在不需要操作栏的活动中使用它
For example you can use a sub style like this
例如,您可以使用这样的子样式
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
While the base theme extends say
虽然基本主题延伸说
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
and then use the non actionbar theme in the AndroidManifest File within the activity tag say
然后在活动标签内的 AndroidManifest 文件中使用非操作栏主题说
<activity
android:name="com.example.NonActionBarActivity"
android:theme="@style/AppTheme.NoActionBar"
you must apply this to each individual activity that don't need actionbar so if your project requires fewer action bar activities than non, then it's better to apply this on the base theme level
您必须将此应用于不需要操作栏的每个单独活动,因此如果您的项目需要的操作栏活动少于非活动,那么最好将其应用于基本主题级别
回答by Thirumalvalavan
I also faced same problem. But I used:
我也面临同样的问题。但我用过:
getSupportActionBar().hide();
getSupportActionBar().hide();
before
前
setContentView(R.layout.activity_main);
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>
回答by Prabhakaran
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.
就是这样。它会起作用。
回答by S HemaNandhini
You need to change
你需要改变
<activity
android:name=".YOUR ACTIVITY"
android:theme="@style/AppTheme.NoActionBar" />
</application>`
these lines in the manifest.It will perfectly work for me.
清单中的这些行。它非常适合我。
回答by Asim Roy
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>
回答by Harry .Naeem
I had toolbar added in my xml. Then in my activity i was adding this statement:
我在我的 xml 中添加了工具栏。然后在我的活动中,我添加了以下声明:
setSupportActionBar(toolbar);
Removing this worked for me. I hope it helps someone.
删除这个对我有用。我希望它可以帮助某人。