Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

styledColor android.content.res.Resources$NotFoundException: Resource ID #0x0 #258

Closed
logan23 opened this issue Oct 20, 2020 · 12 comments · Fixed by #286
Closed

styledColor android.content.res.Resources$NotFoundException: Resource ID #0x0 #258

logan23 opened this issue Oct 20, 2020 · 12 comments · Fixed by #286

Comments

@logan23
Copy link

logan23 commented Oct 20, 2020

3.0.0-beta01 throw Exception
3.0.0-alpha06 works as expected

@LouisCAD
Copy link
Owner

LouisCAD commented Oct 20, 2020

For me, 3.0.0-beta01 works as expected, so I'll need a reproducing snippet and/or additional info at the very least, @logan23.

@logan23
Copy link
Author

logan23 commented Oct 20, 2020

Sure. Sorry for the unclear description.
In alpha06 I can make something like that:

<resources>
    <attr name="test_color" format="color"/>
</resources>

in theme:

<item name="test_color">#E5E5E5</item>

Later somewhere:

val color = styledColor(R.attr.test_color)

And it works, I get my color.
In beta01 I get a crash. If I set color as a reference to color in resource

<item name="test_color">@color/black</item>

everything works well

@LouisCAD
Copy link
Owner

You mean the issue reproduces only when you are not using color reference but when you're using a literal color value?

@logan23
Copy link
Author

logan23 commented Oct 20, 2020

Yes. If that's the intention, then I just don't understand why it worked in alpha.

@LouisCAD
Copy link
Owner

LouisCAD commented Nov 9, 2020

@logan23 Can you test with AGP (the Android Gradle Plugin) 4.2.0-alpha16 or 4.1.1 (once it's released)?

It looks like this issue: https://issuetracker.google.com/issues/170922353

@LouisCAD
Copy link
Owner

@logan23 Did you reproduce with AGP 4.1.1+?

@LouisCAD
Copy link
Owner

LouisCAD commented Jul 6, 2021

@logan23 Can you answer my question, please?

@NEckes
Copy link

NEckes commented Aug 2, 2021

Can reproduce with AGP 7.0.0

Version 3.0.0-alpha06 works
Version 3.0.0-alpha07 breaks

styles.xml

<resources>
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="cLampOffline">#9f00</item>
    </style>
</resources>

attrs.xml

<resources>
    <attr name="cLampOffline" format="color" />
</resources>

call styledColor(R.attr.cLampOnline) in my activity's onCreate leads to following exception:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{... (MyActivity)}: android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:237)
        at android.content.res.Resources.getColor(Resources.java:1059)
        at android.content.Context.getColor(Context.java:677)
        at splitties.resources.ColorResourcesKt.color(ColorResources.kt:24)
        at splitties.resources.ColorResourcesKt.styledColor(ColorResources.kt:66)
        ... (onCreate)

Values from my debugging session: (Android 11 on Emulator)

StyledAttributes#Context.resolveThemeAttribute
isMainThread returned true
theme#resolveAttribute succeeded
uiThreadConfinedCachedTypeValue.resourceId 0
uiThreadConfinedCachedTypeValue.sourceResourceId 0
uiThreadConfinedCachedTypeValue.type 30 (TypedValue.TYPE_INT_COLOR_ARGB4)
uiThreadConfinedCachedTypeValue.data -1711341568, which is Color.valueOf(it) the above mentioned color value

So theme.resolveAttribute resolves to the color itself...

@NEckes
Copy link

NEckes commented Aug 2, 2021

Currently I've written my own utility methods as a temporary replacement

fun Context.styledColor(@AttrRes color: Int) = TypedValue().also { theme.resolveAttribute(color, it, true) }.data
fun appStyledColor(@AttrRes color: Int) = appCtx.styledColor(color)
fun Fragment.styledColor(@AttrRes attr: Int) = requireContext().styledColor(attr)
fun View.styledColor(@AttrRes attr: Int) = context.styledColor(attr)

@LouisCAD
Copy link
Owner

LouisCAD commented Aug 2, 2021

Thanks for your comments @NEckes, I now understand the issue and why I didn't have it.

Basically, you can have theme attribute values that have a direct value, not referencing a resource by its id, and that's what broke.

I'll fix it in 3.0.0-rc02, hopefully coming out in a few hours.

@LouisCAD
Copy link
Owner

LouisCAD commented Aug 2, 2021

I published a snapshot (see instructions on the main project page) that fixes the issue, so you can try it before I make a proper release (it takes a bit longer than I anticipated because I want to handle the edge cases right with good error messages, and not just for color resources).

It's built with the following revision FYI: https://github.com/LouisCAD/Splitties/tree/5b95c4bc72158c285e459a5a6a112d1f2ed08a46

@NEckes
Copy link

NEckes commented Aug 3, 2021

Works on my side ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants