Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources$NotFoundException when accessing an attribute as a color

So there I have a color defined as an attribute because it is dependent on the theme.

<attr name="primary_text_color" format="color"/>

It is defined in the theme as

<style name="BaseReferencesTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="primary_text_color">#ffffffff</item>
</style>

I'd like to wrap it with a real color resource.

<color name="selected_color_normal">?attr/primary_text_color</color>

And then read it from code

int resolvedColor = ContextCompat.getColor(context, R.color.selected_color_normal);

When I do this, I am getting an exception

 android.content.res.Resources$NotFoundException: Resource ID #0x7f06010e type #0x2 is not valid
        at android.content.res.Resources.getColor(Resources.java:955)
        at android.content.Context.getColor(Context.java:588)
        at android.support.v4.content.ContextCompat.getColor(ContextCompat.java:523)

There are a few modules here I work with:

  1. attr_module where the attributes are defined.
  2. theme_module where the theme is defined and set to the application
  3. usage_module which knows nothing about the theme, but does depend on the attr_module.

I know for sure that the theme is applied to the views in the usage_module. All the dependencies are set correctly, when I do not try to programmatically read selected_color_normal, but just applying the attribute - everything is working.

Thanks for any help!

like image 598
ArieDov Avatar asked Oct 02 '18 12:10

ArieDov


2 Answers

For now I am considering this issue not solvable. Currently my approach is to use ?attr/primary_text_color and R.attr.primary_text_color everywhere possible.

like image 145
ArieDov Avatar answered Sep 28 '22 20:09

ArieDov


I think, problem here is because primary_text_color is attribute, and selected_color_normal is color.

Try to make

<color name="selected_color_normal">#FFFFFFFF</color>

And then, in your style you can give primary_text_color value of selected_color_normal if needed:

<style name="BaseReferencesTheme" parent="Theme.AppCompat.NoActionBar">
<item name="primary_text_color">@color/selected_color_normal</item>

like image 25
H.Taras Avatar answered Sep 28 '22 20:09

H.Taras