ContextCompat.getColor() 对于任何定义的颜色返回 -1

KGC*_*beX 5 java sdk android

我无法从资源中检索正确的颜色值。

从 araks 的这个非常好的答案getResources().getColor()中,我们看到新的 Marshmallow API 已弃用旧方法,因此

我的实现:(尽管非常广泛地寻找原因)

int resColorID;
if (//something here) {
    //some code
    resColorID = R.color.color1;
    //some code
    resColorID = R.color.color2;
    //some code
    resColorID = R.color.color3;
}
int resC = ContextCompat.getColor(context, resColorID);
ColorDrawable cDraw = new ColorDrawable(resC);
Run Code Online (Sandbox Code Playgroud)

resC总是,但总是返回-1。对于我的一生,我找不到任何关于此的东西,

指定的颜色(它们是任意名称)R.color.color1等均在中定义colors.xml

额外信息:

  • 最小构建 SDK = 19
  • 编译+目标sdk = 23

为什么会发生这种情况?