Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 资源NotFoundException getColor(),它使用colorAccent属性_Android_Exception - Fatal编程技术网

Android 资源NotFoundException getColor(),它使用colorAccent属性

Android 资源NotFoundException getColor(),它使用colorAccent属性,android,exception,Android,Exception,为什么不能以编程方式解析此颜色: style.xml的定义: <style name="MyTheme.Light" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorAccent">#ff0000</item> </style> 引发以下异常: android.content.res.Resources$NotFoundException: Resource ID #0

为什么不能以编程方式解析此颜色:

style.xml的定义:

<style name="MyTheme.Light" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorAccent">#ff0000</item>
</style>
引发以下异常:

android.content.res.Resources$NotFoundException: Resource ID #0x7f07001b type #0x2 is not valid
at android.content.res.Resources.getColor(Resources.java:752)
...
更改:

style.xml的定义:

<style name="MyTheme.Light" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorAccent">@color/button_color</item>
</style>

这对我有用。您可以用颜色资源替换android.R.attr.colorAccent。

我应该提到这是一个库项目,我的目的是使用父项目的强调色作为按钮的默认颜色,但也允许覆盖该按钮样式。我真正想知道的是为什么这个属性在运行时不能被解析?你有解决这个问题吗?我似乎遇到了类似的问题。。。
android.content.res.Resources$NotFoundException: Resource ID #0x7f07001b type #0x2 is not valid
at android.content.res.Resources.getColor(Resources.java:752)
...
<style name="MyTheme.Light" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorAccent">@color/button_color</item>
</style>
<color name="button_color">#ff0000</color>  
getResources().getColor(R.color.button_color);
TypedValue typedValue = new TypedValue();
TypedArray typedArray = context.obtainStyledAttributes(typedValue.data, new int[] { android.R.attr.colorAccent });
int color = typedArray.getColor(0, 0);