此活動已具有窗口裝飾提供的操作欄

本文翻譯自:This Activity already has an action bar supplied by the window decor

Trying to move over my stuff to use Toolbar instead of action bar but I keep getting an error saying 嘗試移動我的東西以使用Toolbar而不是操作欄,但我不斷收到錯誤消息

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tyczj.weddingalbum/com.xxx.xxx.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:165)
            at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
            at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:113)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

so then I added in my style for my activity to have no actionbar 因此,我添加了自己的風格以使我的活動沒有操作欄

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>
</style>

and the theme is applies to activties in my manifest 這個主題適用於我的清單中的活動

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize|stateHidden"
        android:theme="@style/AppCompatTheme" android:screenOrientation="portrait"/>

MainActivity extends GooglePlayServiceActivity so I also set the theme there too MainActivity擴展了GooglePlayServiceActivity,所以我也在那裏設置了主題

<activity
       android:name=".GooglePlayServicesActivity"
       android:label="@string/title_activity_google_play_services"
       android:theme="@style/AppCompatTheme">

but I still get the error. 但是我仍然得到錯誤。 I also do not request window feature anywhere. 我也不會在任何地方請求窗口功能。 any ideas why I still get this? 任何想法爲什麼我仍然得到這個?


#1樓

參考:https://stackoom.com/question/1nFmE/此活動已具有窗口裝飾提供的操作欄


#2樓

I think you're developing for Android Lollipop, but anyway include this line: 我認爲您正在開發Android Lollipop,但無論如何都包括以下內容:

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

to your theme declaration inside of your app/src/main/res/values/styles.xml . app/src/main/res/values/styles.xml內的主題聲明。

Also, if you're using AppCompatActivity support library of version 22.1 or greater, add this line: 另外,如果您使用的是22.1或更高版本的AppCompatActivity支持庫,請添加以下行:

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

Your theme declaration may look like this after all these additions: 在完成所有這些添加之後,您的主題聲明可能看起來像這樣:

<!-- Base application 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>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

#3樓

Another easy way is to make your theme a child of Theme.AppCompat.Light.NoActionBar like so: 另一個簡單的方法是使您的主題成爲Theme.AppCompat.Light.NoActionBar的子級, Theme.AppCompat.Light.NoActionBar所示:

<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
     ...
</style>

#4樓

Go to the 'style.xml' of your project and make windowActionBar to false 轉到項目的“ style.xml”,並將windowActionBar設置爲false

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>
</style>

#5樓

I also faced same problem. 我也面臨同樣的問題。 But I used: 但是我用過:

getSupportActionBar().hide();

before 之前

setContentView(R.layout.activity_main);

Now it is working. 現在正在工作。

And we can try other option in Style.xml , 我們可以在Style.xml中嘗試其他選項,

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

#6樓

Add this in your values/styles.xml 將此添加到您的values / styles.xml中

<style name="YourCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

<style name="AppTheme" parent="AppBaseTheme">

</style>

And add the following code in your values-v11/styles.xml and values-v14/styles.xml 並在values-v11 / styles.xml和values-v14 / styles.xml中添加以下代碼

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

Thats it. 而已。 It will work. 它會工作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章