This Activity already has an action bar supplied by the window decor
Solution 1
I think you're developing for Android Lollipop, but anyway include this line:
<item name="windowActionBar">false</item>
to your theme declaration inside of your app/src/main/res/values/styles.xml
.
Also, if you're using AppCompatActivity
support library of version 22.1 or greater, add this line:
<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>
Solution 2
Another easy way is to make your theme a child of Theme.AppCompat.Light.NoActionBar
like so:
<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
Solution 3
Add single line android:theme="@style/AppTheme.NoActionBar"
to activity
in AndroidManifest
and you've done.
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>
Solution 4
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
(or its light variant).
Second, create a Toolbar instance, usually via your layout 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” />
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.
Solution 5
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
<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
tyczj
Updated on October 21, 2021Comments
-
tyczj over 2 years
Trying to move over my stuff to use
Toolbar
instead of action bar but I keep getting an error sayingjava.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
<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?
-
tyczj over 9 yearsis
windowActionBar
not the same asandroid:windowActionBar
? -
tyczj over 9 yearsI tried this method too but I still got the error. However that could have been before I set the theme to my
GooglePlayServiceActivity
, I dont remember -
MrEngineer13 over 9 yearsYea, this is what I've been using. Just make sure that the activity is using this theme in the manifest
-
yarian over 9 yearsIt is, but AppCompat stuff looks at the app namespace attribute not the android one. This makes it possible to specify the attribute even in platforms that didn't originally have it. It's how it allows for backporting of things like colorPrimary.
-
tyczj over 9 yearsHow are you helping others when your answer is not correct in reguards to my question
-
voghDev about 9 yearsI find this solution more elegant than @Nadir Belhaj's one, though both of them are correct.
-
rkmax almost 9 yearsWhen I added that line in my styles the app crash :(
-
Christina almost 9 yearsInstead of using "Theme.AppCompat.Light", use "Theme.AppCompat.Light.NoActionBar"
-
Nick over 8 yearsAlso add <item name="windowNoTitle">true</item> If you're using AppCompatActivity after 22.1 support library stackoverflow.com/a/29853299/1402525
-
Booger over 7 yearsI think this is the better answer, using the inline XML attribute (windowActionBar=false) didn't work - I got same error as @tyczj
-
blueware over 7 yearsThis works very well
<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
-
f470071 about 7 yearsIn that case you cannot use Theme.AppCompat.Light.DarkActionBar which actually is a useful parent.
-
CoolMind over 6 yearsThanks! I forgot to add
<android.support.v7.widget.Toolbar
to XML. -
live-love over 6 yearsDon't forget to add: android:theme="@style/AppTheme" to application tag in manifest file.
-
Andrew Irwin about 6 yearsI couldnt figure why my toolbar colour was just blank. Couldnt seem to figure out how to change it to green. I feel silly as all I had to do was put android:background=" " within the <android.support.v7.widget.Toolbar /> in the layout file and that did it! . Thanks
-
Sreekanth Karumanaghat almost 6 yearsWhat if I don't want to remove it? That is there are things that use the toolbar.
-
Harry .Naeem almost 6 yearsI'm not sure about it, but i think removing this "setSupportActionBar(toolbar);" toolbar will stay there.
-
Abbas about 5 yearsI'd also like to add that in my case I had called
setContentView();
twice, stupid I know :). Which frustrated me almost enough to make me mad. -
Sandeep Yohans about 5 yearsperfect! Thanks for sharing.
-
Konstantin F over 3 yearsAlso, I added those lines in values/themes.xml and it worked! Thank you Nadir Belhaj
-
ikmazameti about 3 yearsIt's from the styles and manifest files. This link is easy to fix it examtray.com/android/…
-
Prajwal Waingankar almost 3 yearsThis works pretty well if you want to use Toolbar than simply not use Action bar. Any idea why we cant use both at same time?