Exception setBackgroundColor

Joker99

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>

smdremedy

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

setBackgroundColor is not working

From Dev

setBackgroundColor not working

From Dev

setBackgroundColor is not working

From Dev

setBackgroundColor is deprecated

From Dev

WebView setBackgroundColor not work in Android

From Dev

setBackgroundColor is always turn purple

From Dev

View setBackgroundColor not working android

From Dev

Android setBackgroundColor crashes application

From Dev

.setBackgroundColor with Hex Color Codes AndroidStudio

From Dev

setBackgroundColor changes color of more then the button

From Dev

Android - SetBackgroundColor keeps going to blue?

From Dev

how to set parameter in setBackgroundColor function

From Dev

QListWidget::setStyleSheet() and QListWidgetItem::setBackgroundColor() relation

From Dev

view.setBackgroundColor() is not working in android

From Dev

Textured NSWindow with setBackgroundColor has a tinged grey background

From Dev

UIToolbar setBackgroundColor doesn't fully change color

From Dev

SetBackgroundColor for Button from Resource file in Xamarin (Android)

From Dev

UIToolbar setBackgroundColor doesn't fully change color

From Dev

GWT: SetBackgroundColor of a FormPanel right after starting the program

From Dev

ImageView icon missing when setBackgroundColor is called

From Dev

My setBackgroundColor is Always Different from photoshop

From Dev

setBackgroundColor(Color.WHITE) works in one part of the code, but setBackgroundColor(Color.RED) doesn't

From Dev

Fragmented listview view.setBackgroundColor(Color.GREEN);

From Dev

setBackgroundColor with array stays gray, doesn't change color

From Dev

Why is textview.setBackgroundColor not setting the textviews background, except on virtual devices?

From Dev

setBackgroundColor crashing when loadView implemented for ViewController used for UITabBarController

From Dev

setBackgroundColor on a layout with a unique id also changes the color on other layouts

From Dev

Throw exception or treat exception?

From Dev

Inheritance exception on exist exception

Related Related

  1. 1

    setBackgroundColor is not working

  2. 2

    setBackgroundColor not working

  3. 3

    setBackgroundColor is not working

  4. 4

    setBackgroundColor is deprecated

  5. 5

    WebView setBackgroundColor not work in Android

  6. 6

    setBackgroundColor is always turn purple

  7. 7

    View setBackgroundColor not working android

  8. 8

    Android setBackgroundColor crashes application

  9. 9

    .setBackgroundColor with Hex Color Codes AndroidStudio

  10. 10

    setBackgroundColor changes color of more then the button

  11. 11

    Android - SetBackgroundColor keeps going to blue?

  12. 12

    how to set parameter in setBackgroundColor function

  13. 13

    QListWidget::setStyleSheet() and QListWidgetItem::setBackgroundColor() relation

  14. 14

    view.setBackgroundColor() is not working in android

  15. 15

    Textured NSWindow with setBackgroundColor has a tinged grey background

  16. 16

    UIToolbar setBackgroundColor doesn't fully change color

  17. 17

    SetBackgroundColor for Button from Resource file in Xamarin (Android)

  18. 18

    UIToolbar setBackgroundColor doesn't fully change color

  19. 19

    GWT: SetBackgroundColor of a FormPanel right after starting the program

  20. 20

    ImageView icon missing when setBackgroundColor is called

  21. 21

    My setBackgroundColor is Always Different from photoshop

  22. 22

    setBackgroundColor(Color.WHITE) works in one part of the code, but setBackgroundColor(Color.RED) doesn't

  23. 23

    Fragmented listview view.setBackgroundColor(Color.GREEN);

  24. 24

    setBackgroundColor with array stays gray, doesn't change color

  25. 25

    Why is textview.setBackgroundColor not setting the textviews background, except on virtual devices?

  26. 26

    setBackgroundColor crashing when loadView implemented for ViewController used for UITabBarController

  27. 27

    setBackgroundColor on a layout with a unique id also changes the color on other layouts

  28. 28

    Throw exception or treat exception?

  29. 29

    Inheritance exception on exist exception

HotTag

Archive