2016-03-15 37 views
0

您好,我有以下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> 

回答

1

错误说:

java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor.

因此,由于使用程序兼容性工具栏,在您的Android清单活动代码,则应指定它是这样的:

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

    </application> 
</manifest> 
+1

谢谢,这解决了我的问题。 :) – user3718908

0

主题你正在使用已经有一个动作栏,它也试图将您的工具栏设置为操作栏使用

setSupportActionBar(toolbar); 

这是导致错误。

如果你想为动作条在主题使用

Theme.AppCompat.Light.NoActionBar 

父使用工具栏