3
votes

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?

2
You have metioned your activity extends ActionBarActivity change to ActivityJosef
when i try that i get "Error retrieving parent for item: No resource found that matches the given name 'Theme.Material.Light'."Hector
Theme.Material don't work with ActionBarActivity. Change your v21 theme to AppCompat too and it will workPedro Oliveira
I changed to extend Activity and it works. which is nice, however I still dont get how the you tube video i watched extends from ActionBarActivity and uses Theme.Material.Light for v21 and that works. I f use AppCompat in v21 I am not using Material theme at all?Hector
@Hector I had the same problem... took me ages to notice i needed to use "android:Theme.Material.Light.NoActionBar" instead of "Theme.Material.Light.NoActionBar" (I'm writing this for future users that will run into the same problem).Cookienator

2 Answers

7
votes

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

3
votes

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.