android - 这个 Activity 已经有一个由窗口装饰提供的操作栏?

标签 android xml android-layout android-5.0-lollipop material-design

我正在开发一个 matrial design 的 android 应用程序。我正在尝试使用工具栏作为我的应用程序的操作栏。但是当运行应用程序时我会崩溃并得到错误。

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.

我已经浏览过这些链接但无法正常工作 link link2

style.xml

<style name="AppTheme" parent="AppTheme.Base" />

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDarker</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

<color name="primary">#457C50</color>
<color name="primaryDarker">#580C0C</color>

MianActivity.java

public class MainActivity extends ActionBarActivity {

    Toolbar toolbar;
    DrawerLayout mDrawerLayout;
    FrameLayout mainView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        mainView=(FrameLayout)findViewById(R.id.frame_container);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                toolbar,
                R.string.app_name,
                R.string.app_name
        )

        {
            public void onDrawerClosed(View view)
            {
                super.onDrawerClosed(view);
                invalidateOptionsMenu();
                syncState();
            }

            public void onDrawerOpened(View drawerView)
            {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
                syncState();
            }
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                // TODO Auto-generated method stub
                super.onDrawerSlide(drawerView, slideOffset);
                mainView.setTranslationX(slideOffset * drawerView.getWidth());
                    mDrawerLayout.bringChildToFront(drawerView);
                    mDrawerLayout.requestLayout();

            }
        };
        mDrawerLayout.setDrawerListener(actionBarDrawerToggle);

        //Set the custom toolbar
        if (toolbar != null){
             setSupportActionBar(toolbar);
        }



        actionBarDrawerToggle.syncState();
    }
}

list .xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0"
     >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SET_WALLPAPER" />

    <application
        android:name="com.example.myapp.app.AppController"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
         >
        <activity
            android:name=".Login"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateVisible|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

最佳答案

使用 <item name="windowActionBar">false</item> 代替 <item name="android:windowActionBar">false</item>在你的里面 style.xml

style.xml

<style name="AppTheme" parent="AppTheme.Base" />

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDarker</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

<color name="primary">#457C50</color>
<color name="primaryDarker">#580C0C</color>

关于android - 这个 Activity 已经有一个由窗口装饰提供的操作栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30067946/

相关文章:

java - 什么可能导致 "JAXBElement Does not have a no-arg default constructor"?

android - 通过更改 Android 中的应用程序语言来更改默认手机语言?

android - 卡片 View : Let child item overlap

android - Android 阿拉伯字母的 keyCodes

android - fragment 到 Activity Backstack 返回空白 View

安卓应用程序 : Apply Color Theme Dynamically at Runtime

android - DialogFragment 中带有 LinearLayouts 的 ListView 不包装内容

android - recyclerview 项目大小在布局中大小不一样

android - Espresso - typeText() 无法正常工作,有时会丢失字母

android - Altbeacon Android 库 - didRangeBeaconsInRegion