What causes android exception “You need to use a Theme.AppCompat theme (or descendant) with this activity.”

喜欢而已 提交于 2019-12-01 07:22:53

问题


I am attempting to try out the new Material Theme in Android.

I am currently stuck with this exception

03-06 09:35:50.177: D/AndroidRuntime(30607): Shutting down VM
03-06 09:35:50.178: E/AndroidRuntime(30607): FATAL EXCEPTION: main
03-06 09:35:50.178: E/AndroidRuntime(30607): Process: com.example.vivz, PID: 30607
03-06 09:35:50.178: E/AndroidRuntime(30607): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vivz/com.example.vivz.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.os.Looper.loop(Looper.java:135)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.app.ActivityThread.main(ActivityThread.java:5221)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at java.lang.reflect.Method.invoke(Native Method)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at java.lang.reflect.Method.invoke(Method.java:372)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-06 09:35:50.178: E/AndroidRuntime(30607): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at com.example.vivz.MainActivity.onCreate(MainActivity.java:10)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.app.Activity.performCreate(Activity.java:5933)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-06 09:35:50.178: E/AndroidRuntime(30607):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-06 09:35:50.178: E/AndroidRuntime(30607):    ... 10 more

My main activity extends android.support.v7.app.ActionBarActivity

My res/values/styles.xml resembles this

<resources>

    <style name="Theme.Styled" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

</resources>

My res/values-v21/styles.xml resembles this

<!-- Activity themes -->
<style name="Theme.Styled" parent="android:Theme.Material.Light">
</style>

My application is running on a Nexus 7 with Android 5.0.2

my manifest file looks like this

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Styled" >
        <activity
            android:name=".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>

I can make my application start by deleting the styles.xml file from my res/values-v21 folder.

I do not understand whats wrong and why removing the reference to the one theme I actually want to use fixes the issue.

What have I missed in my rush to try out Material?


回答1:


If you're using ActionBarActivity then you need to use the ThemeCompat styles. You can only use Theme.Material if you use "Activity" instead.

You CAN still use API 21 items like tinting and elevation natively, but in order to use the native material theme you must use the Activity class.

You can still use a Material UI style while using the AppCompat libraries, however: http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html




回答2:


You are attempting to use a feature (from the design library, for example) which was introduced after your target sdk. Therefore, you need to use a couple of libraries (design and appcompat), one of which is the AppCompat Theme (backwards app compatibility).

In your Manifest file, include in your application declaration

android:theme="@style/Theme.AppCompat"

Rebuild and your problem should be solved.



来源:https://stackoverflow.com/questions/28897340/what-causes-android-exception-you-need-to-use-a-theme-appcompat-theme-or-desce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!