Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.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 第二个参数类型错误_Android_Android Layout_Android Studio - Fatal编程技术网

Android 第二个参数类型错误

Android 第二个参数类型错误,android,android-layout,android-studio,Android,Android Layout,Android Studio,我想为我的布局设置背景色。 问题是它给了我: 错误的第二个参数类型。所需整数。 我只是将R.color.red更改为color变量 //add background color header String color = pref.getString("color", null); if(color != null) { color = "R.color." + color; LinearLayout rl =

我想为我的布局设置背景色。 问题是它给了我:

错误的第二个参数类型。所需整数。 我只是将
R.color.red
更改为
color变量

  //add background color header
        String color = pref.getString("color", null);
        if(color != null) {
            color = "R.color." + color;
            LinearLayout rl = (LinearLayout) findViewById(R.id.menuHeader);
            rl.setBackgroundColor(ContextCompat.getColor(this, color));
        }

怎么了?如何解决?

颜色应为int

因此,请将代码更改为:

    String colorString = pref.getString("color", null);
    if(colorString != null) {
        colorString = "R.color." + colorString;

        int myColor = ContextCompat.getColor(this, colorString);

        LinearLayout rl = (LinearLayout) findViewById(R.id.menuHeader);
        rl.setBackgroundColor(ContextCompat.getColor(this, myColor));
    }

颜色
应为int

因此,请将代码更改为:

    String colorString = pref.getString("color", null);
    if(colorString != null) {
        colorString = "R.color." + colorString;

        int myColor = ContextCompat.getColor(this, colorString);

        LinearLayout rl = (LinearLayout) findViewById(R.id.menuHeader);
        rl.setBackgroundColor(ContextCompat.getColor(this, myColor));
    }

ContextCompat.getColor
正在等待类似于
R.color.red
的内容,它实际上是
int
类型。您正在传递一个
字符串,这当然是错误的

你应该这样做:

String color = pref.getString("color", null);
if(color != null) {
      int colorId = this.getResources().getIdentifier(color, "color", this.getPackageName());
      LinearLayout rl = (LinearLayout) findViewById(R.id.menuHeader);
      rl.setBackgroundColor(ContextCompat.getColor(this, colorId));
}
另外,确保如果为
color
获得
X
,则您还有一个名为如下的颜色:
#000000
ContextCompat.getColor
正在等待类似于
R.color.red
的内容,它实际上是
int
类型。您正在传递一个
字符串,这当然是错误的

你应该这样做:

String color = pref.getString("color", null);
if(color != null) {
      int colorId = this.getResources().getIdentifier(color, "color", this.getPackageName());
      LinearLayout rl = (LinearLayout) findViewById(R.id.menuHeader);
      rl.setBackgroundColor(ContextCompat.getColor(this, colorId));
}
另外,确保如果为
color
获得
X
,则您还有一个名为如下的颜色:
#000000

为什么不能保存颜色的字符串值

String color = getResources().getString(R.color.red);
将此保存到共享首选项,因此值将为一些#123456 然后从共享首选项
String color=pref.getString(“color”,null)中获取它

然后rl.setBackgroundColor(Color.parseColor(Color))

为什么不能保存颜色的字符串值

String color = getResources().getString(R.color.red);
将此保存到共享首选项,因此值将为一些#123456 然后从共享首选项
String color=pref.getString(“color”,null)中获取它

然后rl.setBackgroundColor(Color.parseColor(Color))

颜色ir是我在SharedReferences中得到的一个变量。它是红色,蓝色。。。但是要设置颜色,我需要在style.xml
R.color.red
中引用它。问题是,如果我把R.color.red放进去,它会工作,但我需要在那里传递一个dinamic颜色。对不起,我的错误。我添加了一个答案,让我知道这是否适用于您。颜色ir是我在SharedReferences中获得的变量。它是红色,蓝色。。。但是要设置颜色,我需要在style.xml
R.color.red
中引用它。问题是,如果我把R.color.red放进去,它会工作,但我需要在那里传递一个dinamic颜色。对不起,我的错误。我添加了一个答案,让我知道这是否适用于您。哦,但是getColor不受欢迎?是的!使用
ContextCompat.getColor()
然后-让我改变我的答案哦,但是getColor不受欢迎?真的!使用
ContextCompat.getColor()
然后-让我更改我的答案再次感谢!还有一个问题,我可以用它来
设置主题(colorId)也是吗?我需要使用相同的东西,但现在要使用
R.style.Yellow
而不是R.color来更改主题。@RickJoe,是的,您必须执行类似
this.getResources().getIdentifier(styleNameString,“style”,this.getPackageName())
的操作,再次感谢您!还有一个问题,我可以用它来
设置主题(colorId)也是吗?我需要使用相同的东西,但现在要使用
R.style.Yellow
而不是R.color来更改主题。@RickJoe,是的,您必须执行类似
this.getResources().getIdentifier(styleNameString,“style”,this.getPackageName())的操作。