This Activity already has an action bar supplied by the window decor
Asked Answered
U

27

597

Trying to move over my stuff to use Toolbar instead of action bar but I keep getting an error saying

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

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

Undersea answered 22/10, 2014 at 18:59 Comment(0)
C
959

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>
Crummy answered 22/10, 2014 at 19:5 Comment(10)
is windowActionBar not the same as android:windowActionBar?Undersea
It 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.Dominate
When I added that line in my styles the app crash :(Chantress
Also add <item name="windowNoTitle">true</item> If you're using AppCompatActivity after 22.1 support library https://mcmap.net/q/65649/-java-lang-illegalargumentexception-appcompat-does-not-support-the-current-theme-featuresBlacklist
This works very well <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>Lajoie
Don't forget to add: android:theme="@style/AppTheme" to application tag in manifest file.Rhetic
I'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.Justify
Also, I added those lines in values/themes.xml and it worked! Thank you Nadir BelhajWonted
It's from the styles and manifest files. This link is easy to fix it examtray.com/android/…Intercede
Happy it helped all of youCrummy
P
221

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>
Platonism answered 22/10, 2014 at 20:47 Comment(5)
I 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 rememberUndersea
Yea, this is what I've been using. Just make sure that the activity is using this theme in the manifestPlatonism
I find this solution more elegant than @Nadir Belhaj's one, though both of them are correct.Recession
I think this is the better answer, using the inline XML attribute (windowActionBar=false) didn't work - I got same error as @UnderseaR
In that case you cannot use Theme.AppCompat.Light.DarkActionBar which actually is a useful parent.Britneybritni
R
154

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>
Rolo answered 25/12, 2015 at 10:20 Comment(0)
M
57

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.

Marymarya answered 17/7, 2015 at 12:22 Comment(2)
Thanks! I forgot to add <android.support.v7.widget.Toolbar to XML.Cercaria
I 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! . ThanksBabette
G
24

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

Gallaway answered 12/10, 2015 at 15:18 Comment(1)
Thanks! How can I have one custom toolbar use 2 different styles that I set dynamically in the activity, based on my desired mode in the app?Typewritten
D
14

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 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>
Dollfuss answered 29/5, 2015 at 6:42 Comment(0)
E
9

Add this in your 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

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

Thats it. It will work.

Edeline answered 6/7, 2015 at 13:21 Comment(0)
H
7

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.

Handsaw answered 16/12, 2016 at 10:0 Comment(0)
S
7

Use this inside your application tag in manifest or inside your Activity tag.

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

Secretin answered 9/5, 2019 at 7:20 Comment(0)
B
7

Or Simply change the @Style/AppTheme

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

This works for me!

Bilious answered 24/4, 2020 at 12:51 Comment(1)
This 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?Platino
K
6

Go to the 'style.xml' of your project and make windowActionBar to false

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>
</style>
Kal answered 21/12, 2014 at 11:55 Comment(2)
How are you helping others when your answer is not correct in reguards to my questionUndersea
Instead of using "Theme.AppCompat.Light", use "Theme.AppCompat.Light.NoActionBar"Imitation
F
5

If you are using Appcompact Activity use these three lines in your theme.

<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowActionBarOverlay">false</item>
Freehanded answered 7/1, 2016 at 6:5 Comment(0)
B
5

Add this two line in your app theme located in 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

Blanket answered 3/4, 2016 at 13:40 Comment(0)
R
5

If you get the error on this line:

setSupportActionBar(...);

You need to check if your activity is referring to a theme that contains a toolbar. Your application's AppTheme might already contain a toolbar, such as

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

and you are trying to add a second one. If you want to use your application's AppTheme, you need to remove the theme from your activity, in the manifest.xml file.

For example:

<activity
    android:name=".ui.activities.SettingsActivity"
    android:theme="@style/AppTheme1" /> --> remove this theme
Rhetic answered 22/4, 2018 at 13:26 Comment(0)
C
4

I had toolbar added in my xml. Then in my activity i was adding this statement:

setSupportActionBar(toolbar);

Removing this worked for me. I hope it helps someone.

Craniology answered 6/8, 2017 at 12:46 Comment(2)
What if I don't want to remove it? That is there are things that use the toolbar.Verona
I'm not sure about it, but i think removing this "setSupportActionBar(toolbar);" toolbar will stay there.Craniology
F
3

Simply put, you can do the following:-

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

    }
Firstclass answered 4/6, 2016 at 12:0 Comment(0)
A
3

This is how I solved the problem. Add below code in your AndroidMainfest.xml

<activity android:name=".YourClass"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
 </activity>
Asis answered 25/11, 2016 at 9:15 Comment(1)
perfect! Thanks for sharing.Lurlinelusa
A
3

Its very simple you just need to go with any one step out of two..

  1. change your device display mode from Dark mode to normal mode or .
  2. In style.xml if you are using AppCompat then add these 2 lines in your theme/style..<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
Aboulia answered 9/3, 2021 at 17:20 Comment(0)
A
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
Algernon answered 29/1, 2020 at 13:48 Comment(0)
R
2

try to change the parent of application theme

change below in themes.xml

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

to

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

OR below is the correct way to disable the action bar in theme definition

    <style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">    
    <item name="windowActionBar">false</item>
                <item name="windowNoTitle">true</item>
</style>
Rubbery answered 21/10, 2021 at 6:22 Comment(0)
W
2

I have my apps theme derived from Theme.Material3.DayNight.NoActionBar which has <item name="windowActionBar">false</item> and it works fine on all activities.

But as soon as I switch the device to dark mode I get the This Activity already has an action bar supplied by the window decor error.

I have to apply

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

to every activity.

I think this is strange, as the app theme is already defined without action bar. Couldn't yet figure out the reason for it.

Wyeth answered 2/3, 2023 at 10:46 Comment(0)
R
1

I got this error due to a custome attribute inside, removing it fixed the issue.

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
..
        <item name="searchBarBgColor">#383838</item>  --> remove this line
    </style>
Rhetic answered 20/1, 2021 at 3:20 Comment(0)
R
1

It could also be because you have a style without a parent specified:

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

Remove it, and it should fix the problem.

Rhetic answered 20/1, 2021 at 3:36 Comment(0)
S
1

Your app should automatically support DayNight mode if you have this theme:

<style name="Theme.YourProjectName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

However, if you supply a custom action bar via Toolbar in some of your layouts, you have to replace it to:

<style name="Theme.YourProjectName" parent="Theme.MaterialComponents.DayNight.NoActionBar">

Pay attention to you have values/themes.xml and night/themes.xml. You have to keep Theme.MaterialComponents.DayNight.NoActionBar in both this files.

Stonecutter answered 22/8, 2023 at 4:30 Comment(0)
S
0

A lot of answers given here are correct. My re-wording.

If you are explicitly adding appbar/actionbar to your app then you need to turn the default one off. That is if you want to use elements like

<com.google.android.material.appbar.AppBarLayout>
or 
<android.support.v7.widget.Toolbar>

then you need to turn off implicit action bar using correct style.

First thing is to look at the root theme of your application (the one mentioned in the androidmanifest.xml tag.

<application
       xer_main_round"
        android:supportsRtl="true"
        android:theme="@style/myRootTheme">
-----
then your style.xml or theme.xml or similar file should have style that turns off default actionbar. 

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

or you may use explicit style to hide it from the root theme.

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

You may want to go through app-bar documentation https://developer.android.com/training/appbar/setting-up

Straw answered 23/5, 2022 at 5:2 Comment(1)
Thanks! If I have 2 different styles for one custom toolbar, and I want to conditionally set either one of them based on the app mode, how would I do that at runtime?Typewritten
R
-2

Just put parent="Theme.AppCompat.NoActionBar" in your style.xml file

Like - <style parent="Theme.AppCompat.NoActionBar" name="AppTheme.NoActionBar">

Refusal answered 13/12, 2017 at 8:33 Comment(0)
G
-3

I solved it by removing this line:

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

from activity properties in the Manifest file

Gallinacean answered 16/5, 2016 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.