i am Roger Li

Android setup Action Bar 時出現大量錯誤

依照這個Android 官方的 "Adding the Action Bar" Training guide 去加入Action Bar,一不小心可能會錯過一點細節,以致無法順利完成。

Adding the Action Bar 其下有四個Sub Topic包括:

  • Setting Up the Action Bar
  • Adding Action Buttons
  • Styling the Action Bar
  • Overlaying the action Bar

當中如果依照教學去做,去到 "Adding Action Buttons" 尾段你會看到基本的Codes都已填上,但一旦執行程式便可能會立刻失敗,看到大堆Error。
以下是我嘗試Reproduce 的error (為了更傳神,我使用了紅字...keekeekeee)  :

10-17 13:36:09.480: E/AndroidRuntime(32743): FATAL EXCEPTION: main

10-17 13:36:09.480: E/AndroidRuntime(32743): java.lang.RuntimeException: Unable to start activity ComponentInfo{hk.lookfor.demoapp01/hk.lookfor.demoapp01.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.app.ActivityThread.access$700(ActivityThread.java:131)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.os.Handler.dispatchMessage(Handler.java:99)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.os.Looper.loop(Looper.java:137)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.app.ActivityThread.main(ActivityThread.java:4866)

10-17 13:36:09.480: E/AndroidRuntime(32743): at java.lang.reflect.Method.invokeNative(Native Method)

10-17 13:36:09.480: E/AndroidRuntime(32743): at java.lang.reflect.Method.invoke(Method.java:511)

10-17 13:36:09.480: E/AndroidRuntime(32743): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

10-17 13:36:09.480: E/AndroidRuntime(32743): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

10-17 13:36:09.480: E/AndroidRuntime(32743): at dalvik.system.NativeStart.main(Native Method)

10-17 13:36:09.480: E/AndroidRuntime(32743): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:102)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)

10-17 13:36:09.480: E/AndroidRuntime(32743): at hk.lookfor.demoapp01.MainActivity.onCreate(MainActivity.java:23)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.app.Activity.performCreate(Activity.java:5143)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)

10-17 13:36:09.480: E/AndroidRuntime(32743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)

10-17 13:36:09.480: E/AndroidRuntime(32743): ... 11 more

從以上Errorf可看到其中的 "java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity." ,提到需要使用 Theme.AppCompat theme。
其實說明的是在Manifest 內的  要使用 android:theme="@style/Theme.AppCompat" ,而不是default 的 android:theme="@style/AppTheme" 或其他主題。
那是不是Google 的教學出問題呢? Nope! 其實在接差的章節 "Styling the Action Bar" 便會詳細交代有關Styling Theme 的事項。

但如果你點入的頁面是 http://developer.android.com/develop/index.html 的 "API Guides" > "User Interface" > "Action Bar" (http://developer.android.com/guide/topics/ui/actionbar.html) ,相信你不會遇到太大問題了,因為你需要的都在同一頁面上顯示。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *