簡體   English   中英

非法狀態異常。 由於工具欄,活動無法啟動

[英]Illegal state exception. Activity unable to start because of toolbar

我正在嘗試添加一個類似於 soundclouds 的操作欄。 我正在按照有關如何在android 開發者網站上自定義操作欄的說明進行操作。 錯誤似乎是已經提供了操作欄,但我正在嘗試創建另一個。

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.

這是onCreate()

Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
addBirthdayFragment();

這是布局文件:

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

我試圖分別在布局和 onCreate 中刪除工具欄的 xml 代碼和 java 代碼,但這不起作用; 同樣的錯誤仍然存​​在。 這一定與我正在使用的主題有關,但我不知道到底是什么。 這讓我很困惑,因為主題是默認主題,所以我沒想到會這樣。

這是我的主題

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

側面問題:

我正在嘗試實現 soundcloud 操作欄的外觀,它似乎是選項卡式的。 如果我想要這個結果,這是我應該走的方式,還是有其他一些用於選項卡式操作欄的小部件?

感謝您的幫助。

你的代碼是:

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

parent="Theme.AppCompat.Light.DarkActionBar"表示黑暗的操作欄。 您需要將其更改為 NoActionBar。

   <style name="AppTheme" 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>
</style>

Android 正在抱怨,因為您從未告訴它您將使用 Toolbar 而不是 Android 提供的 ActionBar。 要讓 Android 知道這一點,您需要將這些添加到您的樣式中:

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

如果您有時想使用 Android ActionBar 而有時想使用您的 Toolbar,您可以將以上兩行添加到擴展您的 AppTheme 的新樣式中,並在 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM