Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

java - Do not request Window.FEATURE_ACTION_BAR issue

I'm trying to build my app but without success.. I tried several way but nothing was worked. The exception is:

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.

My style.xml is:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light">

        <!-- theme customizations -->

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>

        <item name="android:textColorPrimary">@color/ldrawer_color</item>
        <item name="actionMenuTextColor">@color/ldrawer_color</item>
        <item name="android:textColorSecondary">@color/ldrawer_color</item>
    </style>

</resources>

and as you can see i have declared

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

This is my manifest.xml

 <application
        android:allowBackup="true"
        tools:replace="android:icon"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

and my BaseActivity that i use to extend other activities

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public abstract class BaseActivity extends AppCompatActivity {

    public Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(BaseActivity.this.getResources().getString(R.string.app_name));
            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    protected abstract int getLayoutResource();

    protected void setActionBarIcon(int iconRes) {
        toolbar.setNavigationIcon(iconRes);
    }

}

I don't know why it crashes..The only way to start the application without crash is set the parent on the style.xml in Theme.AppCompat.Light.NoActionBar but in this way the status bar is transparent and not colored...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using Theme.AppCompat.Light tells Android that you want the framework to provide an ActionBar for you. However, you are creating your own ActionBar (a Toolbar), so you are giving the framework mixed signals as to where you want the ActionBar to come from.

Since you are using a Toolbar, you want Theme.AppCompat.Light.NoActionBar.

The next step is to make sure your Toolbar is styled correctly, which seems to be where you are running into issues. To style your Toolbar like an ActionBar using the colors you defined for your theme, you need to provide a theme like so:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

See the "styling" section for the Toolbar widget on this Android Developers blog post for more information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...