问题描述
我在Crashlytics中发现了此错误,并且似乎只有使用Android Nougat预览版的用户才崩溃.
I have found this error in my Crashlytics, and it seems like it's only crashing for users with preview version of Android Nougat.
应用程序在启动时崩溃(主要活动).
App crashes on startup (Main Activity).
Stacktrace
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似乎不再支持我在应用程序中使用的某些系统颜色?但是我不知道该怎么解决.
It looks like Android Nougat no longer supports some kind of system colors that I'm using in my app? But I don't know exactly how to fix it.
修改
所以我在 R 文件中找到了ID为0x7f020057的资源,就是这样:
So I found the resource with ID 0x7f020057 in my R file and this is it:
我检查了我在哪里使用它,这里是:
I checked where I am using it and here it is:
<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"样式,并且应用程序现在可以正常工作,不再崩溃.但是我需要一个更好的解决方案.
I still don't know why this is causing issues on Nougat. I tried removing the "StartingWindowTheme" style and app now works, it no longer crashes. But I need a better fix than that.
编辑2
所以我试图删除此行:
<item name="android:colorBackground">@drawable/background_splash_gradient</item>
它有效.似乎是 android:colorBackground
.
临时修复
由于问题出在上面提到的那行中,所以仅在Nougat上,我创建了一个 values-v24
文件夹并删除了该行.App现在可以在Nougat上运行,但是我希望能有更好的解决方案.
Since the problem is in the line mentioned above, only on Nougat, I have created a values-v24
folder and removed the line there. App works on Nougat now, but I would appreciate a better solution.
推荐答案
我找到了问题所在以及如何解决.这是解决方案,我将使其保持简单.
I found where the problem was and how to fix it. Here's the solution, I'll keep it simple.
此行引起了问题:
<item name="android:colorBackground">@drawable/background_splash_gradient</item>
结果是您无法在XML中将drawable设置为colorBackground,因此在删除此行后即可使用.
Turns out you can't set drawable as a colorBackground in XML, so it works after removing this line.
之所以仅在Nougat上崩溃,是因为在较早的版本中才有可能.
The reason this only crashed on Nougat is because this was possible in earlier versions.
这篇关于仅在Android Nougat上无法从可绘制资源ID中找到ColorStateList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!