是什么原因导致的Andr​​oid例外"你需要使用一个Theme.AppCompat主题(或后代)与此活性QUOT;与此、后代、活性、主题

2023-09-07 00:45:08 作者:[脏话是女汉子的保护色]

我试图尝试在Android上的新材料主题。

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

我目前仍坚持这一例外

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

我的主要活动来延长 android.support.v7.app.ActionBarActivity

我RES /价值/ styles.xml类似于此

My res/values/styles.xml resembles this

<resources>

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

</resources>

我RES /值-V21 / styles.xml类似于此

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

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

我的应用程序在的Nexus 7上运行的Andr​​oid 5.0.2

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>

我可以从我的RES /值-V21夹删除styles.xml文件让我的应用程序启动。

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?

推荐答案

如果你使用ActionBarActivity那么你需要使用ThemeCompat样式。如果使用活动,而不是只能使用Theme.Material。

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

您仍然可以使用API​​ 21的项目,如着色和高程本身,但为了使用原生材料的主题,你必须使用Activity类。

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.

您仍然可以使用材料UI风格,同时使用应用程序兼容性库,但是:的http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-$p$p.html

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