2014-11-23 35 views
7

我是Android編程新手。當我將該工具欄應用於我的應用程序時遇到此問題,並在嘗試運行該應用程序時崩潰。下面是代碼:應用時崩潰工具欄

ToolbarActivity.java 

package com.example.toolbar; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.widget.Toolbar; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 


public class ToolbarActivity extends ActionBarActivity { 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
     if (toolbar != null) { 
      setSupportActionBar(toolbar); 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     } 
    } 


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

} 

activity_toolbar.xml 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <android.support.v7.widget.Toolbar 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/my_awesome_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="?attr/actionBarSize" 
     android:background="?attr/colorPrimaryDark" 

     app:theme="@style/ThemeOverlay.AppCompat.ActionBar"> 

    </android.support.v7.widget.Toolbar> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="page 1" 
     /> 

</LinearLayout> 

,這裏是我的logcat的

11-23 04:46:35.260: E/AndroidRuntime(1870): FATAL EXCEPTION: main 11-23 04:46:35.260: E/AndroidRuntime(1870): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.toolbar/com.example.toolbar.ToolbarActivity}: 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. 
11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.access$600(ActivityThread.java:141) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-23 04:46:35.260: E/AndroidRuntime(1870): at android.os.Looper.loop(Looper.java:137) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.main(ActivityThread.java:5103) 11-23 04:46:35.260: E/AndroidRuntime(1870): at java.lang.reflect.Method.invokeNative(Native Method) 11-23 04:46:35.260: E/AndroidRuntime(1870): at java.lang.reflect.Method.invoke(Method.java:525) 11-23 04:46:35.260: E/AndroidRuntime(1870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
11-23 04:46:35.260: E/AndroidRuntime(1870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-23 04:46:35.260: E/AndroidRuntime(1870): at dalvik.system.NativeStart.main(Native Method) 11-23 04:46:35.260: E/AndroidRuntime(1870): 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. 
11-23 04:46:35.260: E/AndroidRuntime(1870): at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionB‌​arActivityDelegateBase.java:165) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.j‌​ava:92) 11-23 04:46:35.260: E/AndroidRuntime(1870): at com.example.toolbar.ToolbarActivity.onCreate(ToolbarActivity.java:19) 
11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.Activity.performCreate(Activity.java:5133) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 11-23 04:46:35.260: E/AndroidRuntime(1870): ... 11 more 

誰能告訴我是什麼問題?我已經將appcompat_v7與我的項目一起包括在內。謝謝。

回答

15

您可以在logcat的輸出看便知:

不要求Window.FEATURE_ACTION_BAR, 集windowActionBar在你的主題中使用一個工具欄,而不是假的。

同時嘗試使用this linklink

確保您使用:如果你使用Toolbar從V7支持,你應該使用Theme.AppCombat

<item name="windowActionBar">false</item> 

在你的主題

+1

我不太明白髮生。以前我創建了另一個具有類似代碼的項目,它正在工作。 – DanKCl 2014-11-23 10:14:55

+0

我編輯了我的答案 – SacreDeveloper 2014-11-23 10:33:28

+0

它現在工作...謝謝! – DanKCl 2014-11-23 10:58:05

5

Activity

定義你的風格在styles.xml

<style name="AppTheme" parent="Theme.AppCompat"> 
    <item name="colorPrimary">@color/black</item> 
</style> 
<color name="black">#000000</color> 

而且它在AndroidManifest.xml

<application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
+0

隨着你的和@ SacreDeveloper的幫助,它現在工作...謝謝! – DanKCl 2014-11-23 10:59:51

+2

Upvote並接受我的答案,當它幫助,請;) – mklimek 2014-11-23 11:12:55

+0

我實際上無法得到這個建議工作。如果您閱讀[Android文檔](https://developer.android.com/training/appbar/setting-up.html),您會注意到必須使用NoActionBar主題之一來防止應用程序使用操作欄的本地類。 – 2017-07-31 15:10:54

相關問題