549
votes

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?

23

23 Answers

905
votes

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>
211
votes

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>
138
votes

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>
54
votes

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.

23
votes

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

13
votes

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>
8
votes

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.

6
votes

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.

6
votes

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

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

5
votes

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>
4
votes

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

4
votes

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>
4
votes

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.

4
votes

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
4
votes

Or Simply change the @Style/AppTheme

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

This works for me!

2
votes

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

    }
2
votes

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>
1
votes
 <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
0
votes

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>
0
votes

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.

0
votes

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>
-2
votes

I solved it by removing this line:

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

from activity properties in the Manifest file

-2
votes

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

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