Java .setBackgroundResource使我的应用程序崩溃

Java .setBackgroundResource使我的应用程序崩溃,java,android,Java,Android,在应用程序中,每当我尝试选中灰色选项并单击我的按钮时,我的应用程序就会崩溃。所有其他单选按钮都可以正常工作,但每当我尝试通过灰色按钮更改布局的背景色时,应用程序就会崩溃。 当我删除指令Layout.setBackgroundResource(R.color.Gray)时,请注意从代码中,灰色也可以很好地工作,烤成“灰色”。请帮助我如何在应用程序不崩溃的情况下更改所有版面上的背景色 public void setter(View setv) { RelativeLayout instruc

在应用程序中,每当我尝试选中灰色选项并单击我的按钮时,我的应用程序就会崩溃。所有其他单选按钮都可以正常工作,但每当我尝试通过灰色按钮更改布局的背景色时,应用程序就会崩溃。 当我删除
指令Layout.setBackgroundResource(R.color.Gray)时,请注意从代码中,灰色也可以很好地工作,烤成“灰色”。请帮助我如何在应用程序不崩溃的情况下更改所有版面上的背景色

public void setter(View setv)
{
    RelativeLayout instructionlayout = (RelativeLayout)findViewById(R.id.instructionslayout);
    switch(setv.getId())
    {
        case R.id.changecolorbtn:
            int id = SelectedRadioButton();
            switch(id)
            {
                case 1:toost("Original");break;
                case 2:toost("Grey");instructionlayout.setBackgroundResource(R.color.grey);break;//problem is here
                case 3:toost("Red");break;
                case 4:toost("Orange");break;
                case 5:toost("Yellow");break;
                case 6:toost("Green");break;
                case 7:toost("Aqua");break;
                case 8:toost("Marine");break;
                case 9:toost("Purple");break;
                case 10:toost("Silver");break;
                default:toost("Error! - Select An Option!");
            }
            break;
    }
}
public int SelectedRadioButton()
{
    RadioButton original = (RadioButton)findViewById(R.id.origcolor);
    RadioButton grey = (RadioButton)findViewById(R.id.grey);
    RadioButton red = (RadioButton)findViewById(R.id.red);
    RadioButton orange = (RadioButton)findViewById(R.id.orange);
    RadioButton yellow = (RadioButton)findViewById(R.id.yellow);
    RadioButton green = (RadioButton)findViewById(R.id.green);
    RadioButton aqua = (RadioButton)findViewById(R.id.aqua);
    RadioButton marine = (RadioButton)findViewById(R.id.marine);
    RadioButton purple = (RadioButton)findViewById(R.id.purple);
    RadioButton silver = (RadioButton)findViewById(R.id.silver);
    int starter = 1;
    switch(starter)//take advantage of switch falling without break statement
    {
        case 1:if(original.isChecked()){return 1;}
        case 2:if(grey.isChecked()){return 2;}
        case 3:if(red.isChecked()){return 3;}
        case 4:if(orange.isChecked()){return 4;}
        case 5:if(yellow.isChecked()){return 5;}
        case 6:if(green.isChecked()){return 6;}
        case 7:if(aqua.isChecked()){return 7;}
        case 8:if(marine.isChecked()){return 8;}
        case 9:if(purple.isChecked()){return 9;}
        case 10:if(silver.isChecked()){return 10;}
    }
    return -1;//if nothing is checked
}
colors.xml:

<resources>
<color name="original">#25383C</color>
<color name="grey">#484849</color>
<color name="red">#881A27</color>
<color name="orange">#ffa500</color>
<color name="yellow">#CDE707</color>
<color name="green">#00ff00</color>
<color name="aqua">#00FFCC</color>
<color name="marine">#0C0C84</color>
<color name="purple">#630A86</color>
<color name="silver">#c0c0c0</color>
</resources>
我试过:

 View instructionlayout = (View)findViewById(R.id.instructionslayout);
 instructionlayout.setBackgroundColor(R.color.grey);
如何在.setBackground代码之间放置ui线程?


您正在使用setBackgroundColor

yourView.setBackgroundColor(R.color.your_color);
使用以下命令:

instructionlayout.setBackgroundColor(Color.GRAY);
而不是:

instructionlayout.setBackgroundResource(R.color.grey);
试试这个

int color = getResources().getColor(R.color.original);
instructionlayout.setBackgroundColor(color);


请检查
RelativeLayout
的id是否与您引用的id相同


R.id.instructionslayout
可以是与对象名称类似的
R.id.instructionlayout

请使用以下代码。。它对我有用。。我希望它能帮助你

用这个

指令layout.setBackgroundColor(getResources().getColor(R.color.white))

而不是


指示布局。挫折背景资源(R.颜色。灰色)

使用
findViewById
正确检查指令布局视图是否正确绑定。逐行调试它,并检查指令布局是否不为空。我认为您没有使用正确的Id。否则,在活动中,您的代码应该正常工作。
setBackgroundResource
方法也可以使用可绘制、位图、颜色。
activity
中是否有
setter
方法?UI只能在活动主线程上工作。setter方法位于Settings.java中,用于设置布局活动。
int color = getResources().getColor(R.color.original);
instructionlayout.setBackgroundColor(color);
instructionlayout.setBackgroundResource(R.color.original);