Whiz Whiz - 21 days ago 6
Android Question

Can't find ColorStateList from drawable resource ID only on 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)


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.

Edit

So I found the resource with ID 0x7f020057 in my R file and this is it:


public static final int background_splash_gradient=0x7f020057;


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>


And this is the background_splash_gradiend xml file:

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient
android:angle="135"
android:endColor="#00d49e"
android:startColor="#00bcd4"/>

</shape>


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.

Edit 2

So I tried to remove this line:

<item name="android:colorBackground">@drawable/background_splash_gradient</item>


And it works. Seems like
android:colorBackground
is the issue.

TEMPORARY FIX

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.

Answer

I found where the problem was and how to fix it. Here's the solution, I'll keep it simple.

This line was causing the problem:

<item name="android:colorBackground">@drawable/background_splash_gradient</item>

Turns out you can't set drawable as a colorBackground in XML, so it works after removing this line.

The reason this only crashed on Nougat is because this was possible in earlier versions.

Comments