使用Theme.AppCompat时出现Android "Resources$NotFoundException: Resource is not a ColorStateList"错误

4

我正在使用Android Studio,只有在Android Lollipop之前的版本上,我遇到了“Resources$NotFoundException”的问题。以下是异常信息:

Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x2/d=0x7f0100b7 a=2}

R文件中,'0x7f0100b7'指的是'colorAccent'。 我认为这个问题与'appcompat'支持库有关。 我已将其添加到'build.gradle'文件中:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile "com.google.android.gms:play-services-maps:8.4.0"
}

这是主题:
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/bg_main</item>
    <item name="colorPrimaryDark">@color/bg_main_darker</item>
    <item name="colorAccent">@color/bg_dark</item>
</style>

那些颜色存在于 'color.xml' 文件中。

有什么想法吗? 非常感谢。

1个回答

2
您正在使用Material Design特定的属性,但运行的系统版本在Material之前。
注意:如果您的应用程序使用材料主题但没有以这种方式提供替代主题,则您的应用程序将无法在Android 5.0之前的版本上运行。
您必须像此处所述提供替代主题,以在低于5.0的版本上运行。
例如,在res/values/colors.xml中提供:
<!-- extend one of the Theme.AppCompat themes -->
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_blue_500</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorAccent">@color/material_green_A200</item>
</style>

在你的问题文本中,你写的是color.xml而不是colors.xml,这可能也是你问题的一部分 :)

谢谢回复。那么使用支持库的意义何在?我以为它会处理样式属性。 - JamesNickel
支持库为早期版本(2.1以下)提供向后兼容性,包括ActionBar等功能:http://developer.android.com/tools/support-library/features.html#v7 - alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接