George Thompson
George Thompson

Reputation: 7027

Why is the background color not changing when button press?

Problem Below are the xml and main activity files from a simple android program, to change the background color of the screen when the button is clicked.

Why is the code below generating the following error, and what is the best way to fix it? I believe it the problem is something to do with the id activity_main, but am not sure how to resolve it. I have looked at similar problems on this site with out success.

Error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference at com.beutifultec.strobelight.MainActivity.onCreate(MainActivity.java:32)

This is the line

bgElement.setBackgroundColor(Color.RED);

<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.beutifultec.strobelight.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

</android.widget.RelativeLayout>

public class MainActivity extends AppCompatActivity {
public void myButtonListenerMethod() {
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RelativeLayout bgElement = (RelativeLayout)findViewById(R.id.activity_main);
            int color = ((ColorDrawable) bgElement.getBackground()).getColor();
            if (color == Color.RED) {
                bgElement.setBackgroundColor(Color.BLUE);
            }
            else { bgElement.setBackgroundColor(Color.RED);
            }
        }
    });
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.activity_main);
    bgElement.setBackgroundColor(Color.RED);
    myButtonListenerMethod();
}

The exact code works fine if I change the code to change the color of the button instead of the main display.

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button bgElement = (Button) findViewById(R.id.button);
    bgElement.setBackgroundColor(Color.RED);
    myButtonListenerMethod();
}

public void myButtonListenerMethod() {
    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button bgElement = (Button) findViewById(R.id.button);
            int color = ((ColorDrawable) bgElement.getBackground()).getColor();
            if (color == Color.RED) {
                bgElement.setBackgroundColor(Color.BLUE);
            }
            else {
                bgElement.setBackgroundColor(Color.RED);
            }
        }
    });
}
}

Upvotes: 0

Views: 101

Answers (1)

Ajil O.
Ajil O.

Reputation: 6892

Add id for root view, RelativeLayout in this case.

<android.widget.RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"           // <- Add this line here
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.beutifultec.strobelight.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

</android.widget.RelativeLayout>

And change RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.activity_main); to RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.root_layout);

This should work.

Note: Remove the comment from the XML

Upvotes: 1

Related Questions