Tom Finet Tom Finet - 6 months ago 29
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(R.id.main_toolbar);
setSupportActionBar(toolbar);
addBirthdayFragment();


Here is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

</RelativeLayout>


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>
</style>


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

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 -->
</style>

<!-- 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>
</style>