2014-04-22 73 views
0

搜索很多,但沒有任何用處。在Android中自定義標題欄。獲得例外您需要使用Theme.AppCompat主題

我創造我的自定義標題欄一個簡單的應用程序,但不是從天變得那麼遠。我正在使用這個https://developer.android.com/training/basics/actionbar/styling.html

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.title" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="17" /> 

    <application 
     android:theme="@style/CustomActionBarTheme"//title bar 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name="com.example.title.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我創建theme.xml像RES /價值/的themes.xml

代碼:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <!-- the theme applied to the application or activity --> 
    <style name="CustomActionBarTheme" 
      parent="@style/Theme.AppCompat.Light.DarkActionBar"> 
     <item name="android:actionBarStyle">@style/MyActionBar</item> 

     <!-- Support library compatibility --> 
     <item name="actionBarStyle">@style/MyActionBar</item> 
    </style> 

    <!-- ActionBar styles --> 
    <style name="MyActionBar" 
      parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
     <item name="android:background">#CCCCCC</item> 

     <!-- Support library compatibility --> 
    </style> 
</resources> 

獲取異常:( logcat的:

04-22 11:37:21.755: E/test(11652): Exception 
04-22 11:37:21.757: E/AndroidRuntime(11652): FATAL EXCEPTION: main 
04-22 11:37:21.757: E/AndroidRuntime(11652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.title/com.example.title.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.access$600(ActivityThread.java:156) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.os.Looper.loop(Looper.java:153) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.main(ActivityThread.java:5299) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at java.lang.reflect.Method.invokeNative(Native Method) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at java.lang.reflect.Method.invoke(Method.java:511) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at dalvik.system.NativeStart.main(Native Method) 
04-22 11:37:21.757: E/AndroidRuntime(11652): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.example.title.MainActivity.onCreate(MainActivity.java:18) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.Activity.performCreate(Activity.java:5182) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081) 
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270) 
04-22 11:37:21.757: E/AndroidRuntime(11652): ... 11 more 

指導我這個請

+0

檢查這個精心'父=「@風格/部件。 AppCompat.Light.ActionBar.Solid.Inverse「'沒有找到主題 –

回答

1

你MainActivity大概從ActionBarActivity延伸,你是。可能引用了AppCompat,所以你需要d使用Theme.AppCompat

還你android:minSdkVersion="11"。所以,沒有必要擴大ActionBarActivty,無需參考AppCompat,且無需Theme.AppCompat

http://developer.android.com/guide/topics/ui/actionbar.html

還要檢查

https://developer.android.com/training/basics/actionbar/styling.html

+0

是的,我的MainActivity擴展了ActionBarActivity。我應該刪除這個?????解釋我我不明白 – Manwal

+1

@Manwal是的,你需要擴展活動,沒有必要引用AppCompat。檢查文檔那裏有足夠的信息。 ActionBar在api級別11中可用。無需使用支持庫以實現向後兼容性 – Raghunandan

+0

錯誤已修復,但無法更改標題欄的顏色爲什麼? – Manwal

相關問題