android - 在 Lollipop 设备上使用 AppCompat-21 崩溃

标签 android android-studio android-5.0-lollipop android-appcompat build.gradle

最近我将我的 nexus 5 升级到了 Lollipop。然后我使用 AppCompat-21 创建了一个应用程序。 values-v21下的样式如下

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

但是当我运行该应用程序时,它崩溃并显示以下信息:

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

这是我的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'
}

它在 Lollipop 之前的设备下运行良好...

我在谷歌上搜索了很多,没有找到好的解决方案。有人见过这个吗? ps:我正在使用 Android Studio 0.8.14 并且我是 Android Studio 的新手。谢谢。

最佳答案

错误清楚地表明您使用了错误的主题。您正在使用的 Activity 来自 android.support 包,这些 Activity 需要一个 ThemeCompat 主题,而不是 Theme.Material.Light,这是用于 android.app 包中的 Activity 。像这样重写你的主题:

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

这个错误就会消失。

关于android - 在 Lollipop 设备上使用 AppCompat-21 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26989316/

相关文章:

android - 将字符串格式化为 KB、MB 和 GB

android - 将 Dagger2 与数据库一起使用

java - Android webview输入类型文件

android - Android 中的圆形按钮

android - (Android Studio) XML 自动完成在包含模块中不起作用

android-studio - fragment.show(fragmentManager ,“confirmDelete”)错误

Android 5 - 导航栏上的微调器下拉列表

android - 塑造 Android Material Design

android-studio - "Check update"在 android studio 中不起作用

android - 位置无关的可执行文件和 Android