[英]Android exception: You need to use a Theme.AppCompat theme with this activity
[英]Customizing title bar in Android. Getting Exception You need to use a Theme.AppCompat theme
搜索了很多,但沒有發現有用的東西。
我使用自己的自定義標題欄創建了一個簡單的應用程序,但距離現在還很遠。 我正在使用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 / values / 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
請引導我
您的MainActivity可能來自ActionBarActivity。 您可能正在引用AppCompat。 因此,您需要使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.