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

414,513

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

Share:
414,513
tyczj
Author by

tyczj

Updated on October 21, 2021

Comments

  • tyczj
    tyczj over 2 years

    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?

  • tyczj
    tyczj over 9 years
    is windowActionBar not the same as android:windowActionBar?
  • tyczj
    tyczj over 9 years
    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 remember
  • MrEngineer13
    MrEngineer13 over 9 years
    Yea, this is what I've been using. Just make sure that the activity is using this theme in the manifest
  • yarian
    yarian over 9 years
    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.
  • tyczj
    tyczj over 9 years
    How are you helping others when your answer is not correct in reguards to my question
  • voghDev
    voghDev about 9 years
    I find this solution more elegant than @Nadir Belhaj's one, though both of them are correct.
  • rkmax
    rkmax almost 9 years
    When I added that line in my styles the app crash :(
  • Christina
    Christina almost 9 years
    Instead of using "Theme.AppCompat.Light", use "Theme.AppCompat.Light.NoActionBar"
  • Nick
    Nick over 8 years
    Also add <item name="windowNoTitle">true</item> If you're using AppCompatActivity after 22.1 support library stackoverflow.com/a/29853299/1402525
  • Booger
    Booger over 7 years
    I think this is the better answer, using the inline XML attribute (windowActionBar=false) didn't work - I got same error as @tyczj
  • blueware
    blueware over 7 years
    This works very well <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
  • f470071
    f470071 about 7 years
    In that case you cannot use Theme.AppCompat.Light.DarkActionBar which actually is a useful parent.
  • CoolMind
    CoolMind over 6 years
    Thanks! I forgot to add <android.support.v7.widget.Toolbar to XML.
  • live-love
    live-love over 6 years
    Don't forget to add: android:theme="@style/AppTheme" to application tag in manifest file.
  • Andrew Irwin
    Andrew Irwin about 6 years
    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! . Thanks
  • Sreekanth Karumanaghat
    Sreekanth Karumanaghat almost 6 years
    What if I don't want to remove it? That is there are things that use the toolbar.
  • Harry .Naeem
    Harry .Naeem almost 6 years
    I'm not sure about it, but i think removing this "setSupportActionBar(toolbar);" toolbar will stay there.
  • Abbas
    Abbas about 5 years
    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.
  • Sandeep Yohans
    Sandeep Yohans about 5 years
    perfect! Thanks for sharing.
  • Konstantin F
    Konstantin F over 3 years
    Also, I added those lines in values/themes.xml and it worked! Thank you Nadir Belhaj
  • ikmazameti
    ikmazameti about 3 years
    It's from the styles and manifest files. This link is easy to fix it examtray.com/android/…
  • Prajwal Waingankar
    Prajwal Waingankar almost 3 years
    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?