需要帮助解决所述的错误陈述?

时间:2016-08-11 05:00:48

标签: java android

不好意思是一个新手学习安卓工作室,我在那里下载了一个项目here,以便在编写上传到服务器的相机应用程序时弄脏自己。但是在我尝试编译之后我无法让它工作,MainActivity.java文件中的这个语句出错了。

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.color.action_bar))));

Error stated: Expected resource of type string less... (Ctrl+F1) 
This inspection looks at Android API calls that have been annotated with various support annotations (such as RequiresPermission or UiThread) and flags any calls that are not using the API correctly as specified by the annotations.  Examples of errors flagged by this inspection:
Passing the wrong type of resource integer (such as R.string) to an API that expects a different type (such as R.dimen).
Forgetting to invoke the overridden method (via super) in methods that require it
Calling a method that requires a permission without having declared that permission in the manifest
Passing a resource color reference to a method which expects an RGB integer value.

提前致谢。

4 个答案:

答案 0 :(得分:1)

试试这个

actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
不要忘记在项目的res / values / colors.xml文件中设置颜色名称

xml文件

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="action_bar">#fff</color> //write the color you want here
</resources>

答案 1 :(得分:0)

如果您的活动已展开getSupportActionBar(),请使用AppCompatActivity 并且不要使用Color.parseColor()

AppCompatActivity

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));

活动

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));

答案 2 :(得分:0)

你用过这个

getResources().getString(R.color.action_bar)

R.color.action_bar不是字符串值。因此错误Expected resource of type string

您必须使用正确的方法来获取颜色资源。

getResources().getColor(R.color.action_bar)

但是已经弃用了,所以请使用此

ContextCompat.getColor(MainActivity.this,R.color.action_bar)

答案 3 :(得分:0)

ColorDrawable将整数作为参数。你正试图传递字符串。 getString(id)方法返回字符串。 getString此方法使用strings.xml中的字符串作为getString(R.string.yourid)。您应该在/ res / values /文件夹下定义colors.xml。您可以阅读如下:getColor(R.color.action_bar)。不推荐使用此方法。你应该使用

  

ContextCompat.getColor(yourcontext,R.color.action_bar)

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(ContextCompat.getColor(this,R.color.action_bar))));
相关问题