在库模块中添加工具栏

时间:2016-10-11 21:34:41

标签: android android-toolbar illegalstateexception

我在我的图书馆项目的活动中添加工具栏。在我的AndroidManifest库中,我正在使用这个主题 -

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

但是由于AppTheme中的冲突导致了明显合并中的一些问题,我添加了将我的app模块的AndroidManifest更改为此 -

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

然而,由于我的“活动”库主题被替换为仍然使用Theme.AppCompat.Light.DarkActionBar作为父主题的应用程序模块,因此默认的ActionBar仍然存在,当我尝试将ToolBar设置为我的ActionBar时库Activity使用以下行 -

setSupportActionBar(mToolbar);

我收到以下例外 -

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

我试过调用

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

对例外没有影响。我可以在我的库Activity中使用工具栏吗?

1 个答案:

答案 0 :(得分:1)

好的,真的很傻。我在库清单中的Application元素中设置主题,而我应该在Activity上设置它。

正确的方式

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

错误的方式

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