Search code examples
androidandroid-toolbarillegalstateexception

Adding Toolbar in Library Module


I am adding a Toolbar in an Activity in my Library project. In my library AndroidManifest, I am using this theme -

<style name="NoobAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item>
</style>

But since it was giving some problem in manifest merger due to conflict in AppTheme, I added the changed the AndroidManifest of my app module to this -

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:theme"> <!-- This line solves the merger issue -->

However since my Library theme for the Activity is getting replaced by the app module which is still using Theme.AppCompat.Light.DarkActionBar as a parent theme, the default ActionBar is still there and when I try to set the ToolBar as my ActionBar in the library Activity using the following line -

setSupportActionBar(mToolbar);

I am getting the following Exception -

--------- beginning of crash
10-12 02:54:32.171 28558-28558/noob.com.noobfilechooser E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: noob.com.noobfilechooser, PID: 28558
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{noob.com.noobfilechooser/com.noob.noobfilechooser.NoobFileActivity}: 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.
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                              at android.app.ActivityThread.access$900(ActivityThread.java:153)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5451)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                           Caused by: 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.
                                                                              at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:199)
                                                                              at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
                                                                              at com.noob.noobfilechooser.NoobFileActivity.onCreate(NoobFileActivity.java:60)
                                                                              at android.app.Activity.performCreate(Activity.java:6323)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                              at android.app.ActivityThread.access$900(ActivityThread.java:153) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:148) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5451) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

I have tried calling

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

which have no effect on the exception. Is there anything I can do to use Toolbar in my library Activity?


Solution

  • OK, it was really silly. I was setting the theme in my Application element in the library manifest, while I should have been setting it on the Activity instead.

    Correct way

    <application
            android:allowBackup="true"
            android:label="@string/app_name"
            android:supportsRtl="true">
            <activity android:theme="@style/NoobAppTheme"
                android:name=".NoobFileActivity">
            </activity>
    </application>
    

    Wrong way

    <application
            android:allowBackup="true"
            android:label="@string/app_name"
            android:theme="@style/NoobAppTheme"
            android:supportsRtl="true">
            <activity android:name=".NoobFileActivity">
            </activity>
    </application>