Tom Finet Tom Finet - 2 years ago 114
Java Question

Illegal state exception. Activity unable to start because of toolbar

I am trying to add an actionbar similar to soundclouds. I am following the instructions on how to customize the action bar on the android developer website. The error seems to be that the action bar is already being supplied but I am trying to create another one.

java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

Here is onCreate():

Toolbar toolbar = (Toolbar) findViewById(;

Here is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""



I have tried to just remove the xml code and java code for the toolbar, in the layout and onCreate respectively, however this is not working; the same error still persists. It must be something to do with the theme I am using, but I do not know what exactly. It confuses me because the theme is the default one so I did not expect this.

Here is my 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>

Side question:

I am trying to achieve the look of the soundcloud action bar, which appears to be tabbed. Is this the way I should go if I want this result, or is there some other widget that is used for tabbed action bars?

Thank you for the help.

Answer Source

Android is complaining because you never told it that you will use a Toolbar instead of the Android-provided ActionBar. To let Android know about this, you need to add these to your style:

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

If you'd like to use the Android ActionBar sometimes and a Toolbar of yours some other time, you can add the above two lines to a new style extending your AppTheme, and using that on AndroidManifest.xml:

<!-- Use this one in the Manifest when you want to use the native ActionBar -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- your styling here -->

<!-- Use this one when you want to implement your own Toolbar -->
<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download