此Activity已经提供了一个操作栏错误android

时间:2018-02-05 06:01:22

标签: java android xml android-navigation-drawer

我第一次尝试在我的应用中创建导航抽屉。我创建了一个新项目并选择导航抽屉活动作为主要活动。我只是以此为例。一切似乎都已完成,但是当我运行应用程序时,它会遇到这个确切的错误

Process: com.eduard.butka.songs, PID: 14469

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eduard.butka.songs/com.eduard.butka.songs.MainActivity}: 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:2763)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2824)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1546)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6351)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
     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:201)
        at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
        at com.eduard.butka.songs.MainActivity.onCreate(MainActivity.java:79)
        at android.app.Activity.performCreate(Activity.java:6775)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2716)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2824) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1546) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6351) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) 

我看了一些其他的答案,但我没找到任何东西,我很困惑,因为我复制了android工作室给你的大部分内容,但是我的应用程序崩溃了,而示例运行得很好。这是我的onCreate()

@Override
protected void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);            //this is line 79, where the app crashes

    DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
 }

这是我的styles.xml

<resources>

<style name="AppTheme" parent="AppTheme.Base"></style>
<!-- Base application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowContentTransitions">true</item>

    <item name = "android:windowEnterTransition">@android:transition/fade</item>
    <item name = "android:windowExitAnimation">@android:transition/fade</item>

    <item name = "android:windowAllowEnterTransitionOverlap">true</item>
    <item name = "android:windowAllowReturnTransitionOverlap">true</item>
    <item name = "android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name = "android:windowSharedElementExitTransition">@android:transition/move</item>

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:textColorHint">@color/colorHint</item>

    <item name="android:orientation">vertical</item>

</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name ="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name ="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark"/>

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
</style>
</resources>

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<include
    layout="@layout/action_bar_main"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer"
    />
 </android.support.v4.widget.DrawerLayout>

我查看了所有内容,并将所有内容与AndroidStudio示例进行了比较,但找不到任何内容。

1 个答案:

答案 0 :(得分:3)

在清单或MainActivity标记内的应用程序标记内使用此内容。

  

机器人:主题=&#34; @风格/ AppTheme.NoActionBar&#34;