仅在 Android Nougat 上无法从可绘制资源 ID 中找到 ColorStateList
Can't find ColorStateList from drawable resource ID only on Android Nougat
我在我的 Crashlytics 中发现了这个错误,似乎只有 Android Nougat 预览版的用户才会崩溃。
应用程序在启动时崩溃(主要 Activity)。
堆栈跟踪
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.domain/com.my.domain.activities.MainActivity}: android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x7f020057
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x7f020057
at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:840)
at android.content.res.Resources.loadColorStateList(Resources.java:998)
at android.content.res.TypedArray.getColor(TypedArray.java:447)
at android.app.Activity.onApplyThemeResource(Activity.java:4039)
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:198)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:140)
at android.app.Activity.setTheme(Activity.java:4009)
at android.support.v7.app.AppCompatActivity.setTheme(AppCompatActivity.java:90)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2592)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
看起来 Android Nougat 不再支持我在我的应用程序中使用的某种系统颜色?但是我不知道具体怎么解决。
编辑
所以我在 R 文件中找到了 ID 为 0x7f020057 的资源,就是这样:
public static final int background_splash_gradient=0x7f020057;
我检查了我在哪里使用它,这里是:
<style name="StartingWindowTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/background_splash_gradient</item>
<item name="android:colorBackground">@drawable/background_splash_gradient</item>
</style>
这是 background_splash_gradiend xml 文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="135"
android:endColor="#00d49e"
android:startColor="#00bcd4"/>
</shape>
我仍然不知道为什么这会导致牛轧糖出现问题。我尝试删除 "StartingWindowTheme" 样式,应用程序现在可以运行,不再崩溃。但我需要比这更好的解决方法。
编辑 2
所以我试图删除这一行:
<item name="android:colorBackground">@drawable/background_splash_gradient</item>
并且有效。似乎 android:colorBackground
是问题所在。
临时修复
由于问题出在上面提到的行中,仅在牛轧糖上,我创建了一个 values-v24
文件夹并删除了那里的行。应用程序现在可以在牛轧糖上运行,但我希望有更好的解决方案。
我找到了问题所在以及解决方法。这是解决方案,我会保持简单。
这一行导致了问题:
<item name="android:colorBackground">@drawable/background_splash_gradient</item>
原来你不能在 XML 中将 drawable 设置为 colorBackground,所以在删除此行后它可以工作。
这只在 Nougat 上崩溃的原因是因为这在早期版本中是可能的。
事实证明 according with the Android issue tracker,这是按预期工作的。
android:colorBackground
期望颜色不是可绘制的。
我在我的 Crashlytics 中发现了这个错误,似乎只有 Android Nougat 预览版的用户才会崩溃。
应用程序在启动时崩溃(主要 Activity)。
堆栈跟踪
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.domain/com.my.domain.activities.MainActivity}: android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x7f020057
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x7f020057
at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:840)
at android.content.res.Resources.loadColorStateList(Resources.java:998)
at android.content.res.TypedArray.getColor(TypedArray.java:447)
at android.app.Activity.onApplyThemeResource(Activity.java:4039)
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:198)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:140)
at android.app.Activity.setTheme(Activity.java:4009)
at android.support.v7.app.AppCompatActivity.setTheme(AppCompatActivity.java:90)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2592)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
看起来 Android Nougat 不再支持我在我的应用程序中使用的某种系统颜色?但是我不知道具体怎么解决。
编辑
所以我在 R 文件中找到了 ID 为 0x7f020057 的资源,就是这样:
public static final int background_splash_gradient=0x7f020057;
我检查了我在哪里使用它,这里是:
<style name="StartingWindowTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/background_splash_gradient</item>
<item name="android:colorBackground">@drawable/background_splash_gradient</item>
</style>
这是 background_splash_gradiend xml 文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="135"
android:endColor="#00d49e"
android:startColor="#00bcd4"/>
</shape>
我仍然不知道为什么这会导致牛轧糖出现问题。我尝试删除 "StartingWindowTheme" 样式,应用程序现在可以运行,不再崩溃。但我需要比这更好的解决方法。
编辑 2
所以我试图删除这一行:
<item name="android:colorBackground">@drawable/background_splash_gradient</item>
并且有效。似乎 android:colorBackground
是问题所在。
临时修复
由于问题出在上面提到的行中,仅在牛轧糖上,我创建了一个 values-v24
文件夹并删除了那里的行。应用程序现在可以在牛轧糖上运行,但我希望有更好的解决方案。
我找到了问题所在以及解决方法。这是解决方案,我会保持简单。
这一行导致了问题:
<item name="android:colorBackground">@drawable/background_splash_gradient</item>
原来你不能在 XML 中将 drawable 设置为 colorBackground,所以在删除此行后它可以工作。
这只在 Nougat 上崩溃的原因是因为这在早期版本中是可能的。
事实证明 according with the Android issue tracker,这是按预期工作的。
android:colorBackground
期望颜色不是可绘制的。