Joker99
Joker99

Reputation: 37

Exception setBackgroundColor

I have a question for my code, it should change the background color of my Start-Activity. I should choose between blue and red with radio buttons (in RadioGroup). I always get an Error when i click on the RadioButton Red/Blue in the settings menue.

These two lines are wrong?

ChangeToRed(background); background.setBackgroundColor(0x0000FF00);

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference at com.example.clecks.reaction_game.activity_settings.ChangeToBlue(activity_settings.java:67) at com.example.clecks.reaction_game.activity_settings$2.onCheckedChanged(activity_settings.java:42)

Here is my java code:

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 Code:

<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>

Upvotes: 2

Views: 11820

Answers (2)

smdremedy
smdremedy

Reputation: 288

You have no R.id.start in your layout, so findViewById(R.id.start) is returning null.

Upvotes: 3

Jordi Castilla
Jordi Castilla

Reputation: 27003

As your exception says, the method RelativeLayour.setBackgroundColor expects an int

Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference at com.example.clecks.reaction_game.activity_settings.changeToBlue

find here setBackgroundColor method documentation

Instead of hardcoding the color, you can use:

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

}

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

}

Check here all Color references.

Upvotes: 0

Related Questions