尝试导航到活动时主题操作栏错误

时间:2016-03-15 23:33:12

标签: java android

您好我有以下 activity_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/coordinatorLayout"
    tools:context="com.xxxxxx.eventmanager.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/eventListView"
        android:layout_centerHorizontal="true" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

使用以下 ActivityMain.java 类:

package com.xxxxxx.eventmanager;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

import com.xxxxxx.eventmanager.models.UpdaterModel;
import com.xxxxxx.eventmanager.services.UpdaterService;

public class MainActivity extends AppCompatActivity {

    // Shared Preferences
    private SharedPreferences settings;
    public static Context contextOfApplication;
    private UpdaterModel mUpdater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        settings = getSharedPreferences("App", MODE_PRIVATE);

        Intent ishintent = new Intent(this, UpdaterService.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
        AlarmManager alarm = (AlarmManager)getSystemService(this.ALARM_SERVICE);
        alarm.cancel(pintent);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pintent);

        contextOfApplication = getApplicationContext();

        mUpdater = new UpdaterModel(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_settings:
                Toast.makeText(MainActivity.this, "You selected the settings menu!", Toast.LENGTH_SHORT).show();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void logEventsTask (View view) {
        Log.i("events", mUpdater.getEventData().toString());
    }

    public static Context getContextOfApplication(){
        return contextOfApplication;
    }

}

现在,当我尝试从我的LoginActivity导航到此活动时,如下所示:

Intent i = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(i);

随之而来的是这个错误:

FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxx.eventmanager/com.xxxxxx.eventmanager.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:2059)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
         at android.app.ActivityThread.access$600(ActivityThread.java:130)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:4745)
         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:786)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
         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_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
         at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
         at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
         at com.xxxxxx.eventmanager.MainActivity.onCreate(MainActivity.java:34)
         at android.app.Activity.performCreate(Activity.java:5008)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
         at android.app.ActivityThread.access$600(ActivityThread.java:130) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
         at android.os.Handler.dispatchMessage(Handler.java:99) 
         at android.os.Looper.loop(Looper.java:137) 
         at android.app.ActivityThread.main(ActivityThread.java:4745) 
         at java.lang.reflect.Method.invokeNative(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:511)

我是Java和Android编程的新手,所以我遇到了麻烦,目前还不知道如何解决这个问题。

这是我的 styles.xml 文件:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.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>
    <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.Light" />
</resources>

2 个答案:

答案 0 :(得分:1)

错误说:

  

java.lang.IllegalStateException:此Activity已经有一个动作   窗户装饰提供的酒吧。

因此,既然您使用了appcompat工具栏,那么在您的android清单活动标记中,您应该像这样指定它:

<manifest>
    ...
    <application>
        ....
        <activity 
            android:name="com.xxxxxx.eventmanager.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"/>

    </application>
</manifest>

答案 1 :(得分:0)

您正在使用的主题已经有一个操作栏,并且您尝试使用

将工具栏设置为操作栏
setSupportActionBar(toolbar);

导致错误。

如果要将工具栏用作ActionBar,请使用

Theme.AppCompat.Light.NoActionBar

作为主题中的父级