将属性作为颜色访问时的 Resources$NotFoundException

Ari*_*Dov 7 resources android

因此,我将颜色定义为属性,因为它取决于主题。

<attr name="primary_text_color" format="color"/>
Run Code Online (Sandbox Code Playgroud)

它在主题中定义为

<style name="BaseReferencesTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="primary_text_color">#ffffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我想用真实的颜色资源包装它。

<color name="selected_color_normal">?attr/primary_text_color</color>
Run Code Online (Sandbox Code Playgroud)

然后从代码中读取

int resolvedColor = ContextCompat.getColor(context, R.color.selected_color_normal);
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到了一个例外

 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)
Run Code Online (Sandbox Code Playgroud)

我在这里使用了几个模块:

  1. attr_module 定义属性的地方。
  2. theme_module 定义主题并将其设置为应用程序的位置
  3. usage_module它对主题一无所知,但确实取决于attr_module.

我确信该主题适用于usage_module. 所有依赖项都设置正确,当我不尝试以编程方式 read selected_color_normal,而只是应用属性时 - 一切正常。

谢谢你的帮助!

Ari*_*Dov 3

目前我认为这个问题无法解决。?attr/primary_text_color目前我的方法是在任何可能的地方使用R.attr.primary_text_color