Java 异常设置背景色

Java 异常设置背景色,java,android,nullpointerexception,Java,Android,Nullpointerexception,我对我的代码有一个问题,它应该更改我开始活动的背景色。我应该用单选按钮(在RadioGroup中)在蓝色和红色之间进行选择。当我点击设置菜单中的红色/蓝色单选按钮时,总是会出现错误 这两行是错的 ChangeToRed(background); background.setBackgroundColor(0x0000FF00); java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.widget.RelativeLayout.se

我对我的代码有一个问题,它应该更改我开始活动的背景色。我应该用单选按钮(在RadioGroup中)在蓝色和红色之间进行选择。当我点击设置菜单中的红色/蓝色单选按钮时,总是会出现错误

这两行是错的

ChangeToRed(background); background.setBackgroundColor(0x0000FF00);
java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.widget.RelativeLayout.setBackgroundColor(int)” 在com.example.clecks.reaction\u game.activity\u settings.ChangeToBlue(activity\u settings.java:67) 在com.example.clecks.reaction\u game.activity\u settings$2.onCheckedChanged(activity\u settings.java:42)

以下是我的java代码:

public class activity_settings extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_settings);
    ColorChange();
}

public void ColorChange() {
    final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);
    final RadioButton ChangeToBlue = (RadioButton) findViewById(R.id.button_blue);
    final RadioButton ChangeToRed = (RadioButton) findViewById(R.id.button_red);


    final Button button_save = (Button) findViewById(R.id.button_save);
    button_save.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            ChangeOption(background);
        }
    });


    ChangeToBlue.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            ChangeToBlue(background);
        }
    });

    ChangeToRed.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            ChangeToRed(background);
        }

    });


}

public void ChangeOption(RelativeLayout background) {
    if (background.isEnabled()) {
        background.setEnabled(false);
    } else {
        background.setEnabled(true);

    }
}

public void ChangeToBlue(RelativeLayout background) {
    background.setBackgroundColor(Color.BLUE);
    background.invalidate();

}
public void ChangeToRed(RelativeLayout background) {
    background.setBackgroundColor(Color.RED);
    background.invalidate();

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_activity_settings, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}
XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.clecks.reaction_game.activity_settings">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Einstellungen"
        android:id="@+id/textView2"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/textView3"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Singalton"
        android:id="@+id/textView4"
        android:layout_gravity="center_horizontal" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton"
            android:layout_gravity="center_horizontal"
            android:checked="false" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton2"
            android:layout_gravity="center_horizontal" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView5"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Hintergrundfarbe"
        android:id="@+id/textView6"
        android:layout_gravity="center_horizontal" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:id="@+id/RadioGroup_Color">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rot"
            android:id="@+id/button_red"
            android:checked="false"
            android:layout_gravity="center_horizontal" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Blau"
            android:id="@+id/button_blue"
            android:checked="false"
            android:layout_gravity="center_horizontal" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Grün"
            android:id="@+id/button_green"
            android:checked="false"
            android:layout_gravity="center_horizontal" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView7"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Spielmodus"
        android:id="@+id/textView8"
        android:layout_gravity="center_horizontal" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Handy"
            android:id="@+id/radioButton6"
            android:layout_gravity="center_horizontal"
            android:checked="false" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Wii-Mote"
            android:id="@+id/radioButton7"
            android:layout_gravity="center_horizontal"
            android:checked="false" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView9"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView10"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Speichern"
        android:id="@+id/button_save"
        android:layout_gravity="center_horizontal" />

</LinearLayout>


正如您的例外情况所述,方法
相对.setBackgroundColor
需要
int

尝试在com.example.clecks.reaction\u game.activity\u settings.changeToBlue上的空对象引用上调用虚拟方法“void android.widget.RelativeLayout.setBackgroundColor(int)”

不必硬编码颜色,您可以使用:

public void changeToBlue(RelativeLayout background) {
    background.setBackgroundColor(Color.BLUE);
    background.invalidate();

}

public void changeToRed(RelativeLayout background) {
    background.setBackgroundColor(Color.RED);
    background.invalidate();

}

.

您的布局中没有
R.id.start
,因此
findViewById(R.id.start)
返回null。

检查背景变量是否不为null如何。另外,尝试对类使用java命名约定start是另一个活动,我想我可以使用以下行更改start活动背景的颜色:O