Hector Hector - 27 days ago 13
Android Question

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

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?

Answer

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