Using AppCompat-21 on Lollipop Device Crashes

I recently upgraded my link 5 to Lollipop. and I am creating an application using AppCompat-21. Styles under values-v21 look like follo

<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light">
    </style>
</resources>

      

but when i run the app it crashes with information:

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

      

here is my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "geone.businspector"
        minSdkVersion 11
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.0'
}

      

It works fine under a pre-Lollipop device ...

I've googled a lot and didn't have a good solution. has anyone met this before? ps: I am using Android Studio 0.8.14 and I am new to Android Studio. THX.

+3


source to share


3 answers


This error clearly states that you are using the wrong theme. The actions you use are from the package android.support

, these actions require a theme ThemeCompat

, not the Theme.Material.Light

one that is for the actions in the package android.app

. Rewrite your topic like this:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat">
    </style>
</resources>

      



And this error will disappear.

+5


source


Under values ​​/ styles.xml you will be using Theme.AppCompat.Light Use same parent theme for your values-v21 / styles.xml . This will save you the error as it did for me.



On Lollipop devices, the theme works as if it really isn't worth worrying about :)

+1


source


So I had the same problem and got tired of it and saw people say they don't use Subject material. Don't use Material Theme changes the action bar to gray even if you set a color for it in styles.

Anyway, the fix is ​​that android templates created in android studio use import android.support.v7.app.ActionBarActivity; instead of importing android.app.Activity; and extend ActionBarActivity instead of Activity for main class. Just change the main class to extend Activity instead of ActionBarActivity. Of course, no more changes are needed unless you are using Blank activity.

0


source







All Articles