简体   繁体   English

Android发布错误:预期颜色资源ID(R.color。)但收到RGB整数

[英]Android Release error: Expected a color resource id (R.color.) but received an RGB integer

I have an application in the market for a year. 我在市场上申请了一年。 Last week, I have changed the source code of my app. 上周,我更改了我的应用程序的源代码。 When I wanted to build the release version Android Studio throws an Error: 当我想构建发布版本时,Android Studio会抛出一个错误:

"Error: Expected a color resource id (R.color.) but received an RGB integer [ResourceType]" “错误:预期颜色资源ID(R.color。)但收到RGB整数[ResourceType]”

Color is only used in this part of code and I haven't made ​​any changes in this part: 颜色仅用于此部分代码中,我没有对此部分进行任何更改:

if (android.os.Build.VERSION.SDK_INT >= 16) {
            rlFlash.setBackground(new ColorDrawable
                    (Color.parseColor(("#86cc55"))));
        }
        else{
            rlFlash.setBackgroundDrawable(new ColorDrawable
                    (Color.parseColor(("#86cc55"))));
        }

It is so strange that in the Debug version Android studio didn't throw any error and I can build the apk. 很奇怪,在调试版Android Studio中没有抛出任何错误,我可以构建apk。

Do you know whats happen?? 你知道什么事发生?

Thanks. 谢谢。

像这样做:

rlFlash.setBackgroundColor(Color.parseColor("#86cc55"));

Though it's too late, but this answer is for those who may still get this error while taking release build like me. 虽然为时已晚,但这个答案适用于那些在像我这样的版本构建时仍然会出现此错误的人。 Don't know if it's a solution or just workaround. 不知道这是一个解决方案还是只是解决方法。

@SuppressLint("ResourceType")

Use this at your method signature like below where you get the error. 在您的方法签名中使用此选项,如下所示,您将收到错误。

@SuppressLint("ResourceType")
public void aMethodWhereYouMayGetTheError(){
}

By using this you will be able to take release build without that error. 通过使用它,您将能够在没有该错误的情况下进行发布构建。

If you have used the anotation @ColorRes in this method then remove that. 如果您在此方法中使用了anotation @ColorRes,则删除它。 The apk will be generated successfully. apk将成功生成。 Refer to SO Answer to clarify further about the lint checks. 请参阅SO答案以进一步阐明棉绒检查。 The answer is quoted below: 答案如下:

There are Java annotations to support these checks in your own code. Java注释可以在您自己的代码中支持这些检查。 They can all be found in the android.support.annotations package: IdRes DrawableRes LayoutRes StringRes ColorRes &c In this case, for example, I could use: 它们都可以在android.support.annotations包中找到:IdRes DrawableRes LayoutRes StringRes ColorRes&c在这种情况下,例如,我可以使用:

private void mySetContentView(@LayoutRes int resourceId) {
    setContentView(resourceId); 
} 

and Android Studio will check that the provided resource id is indeed for a layout. Android Studio将检查提供的资源ID是否确实用于布局。 Moreover, these annotations are exported, so they can be especially useful when designing a library. 而且,这些注释被导出,因此在设计库时它们特别有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM