在库模块中添加工具栏

Adding Toolbar in Library Module

我要在我的库项目的 Activity 中添加一个工具栏。在我的 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 中的冲突导致清单合并出现一些问题,我将我的应用模块的 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 -->

然而,由于我的 Activity 库主题被仍然使用 Theme.AppCompat.Light.DarkActionBar 作为父主题的应用程序模块所取代,默认的 ActionBar 仍然存在,当我尝试设置时在库 Activity 中将 ToolBar 作为我的 ActionBar 使用以下行 -

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.access0(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.access0(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?

好吧,这真的很傻。我在库清单中的应用程序元素中设置主题,而我应该在 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>