-2
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference.

This is what I get when I run the program and I don't know what to do. I read the others post but still don't know what the problem is.I replaced the "v" in "public void onClick(View v)" with "findViewById" but still no working.

package com.example.gia.applumini;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;



public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout bgElement = (RelativeLayout) findViewById(R.layout.activity_main);
        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) {
                RelativeLayout bgElement = (RelativeLayout) findViewById(R.layout.activity_main);
                int color = ((ColorDrawable) bgElement.getBackground()).getColor();
                if (color == Color.RED) {
                    bgElement.setBackgroundColor(Color.BLUE);
                }
                else {
                    bgElement.setBackgroundColor(Color.RED);
                }
            }
        });
    }
}
Rob
  • 14,746
  • 28
  • 47
  • 65

3 Answers3

0

Try to adjust your code to be like the following :

public class MainActivity extends AppCompatActivity {

RelativeLayout bgElement;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bgElement = (RelativeLayout) findViewById(R.id.RELATIVELAYOUTID);
        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) {
                int color = ((ColorDrawable) bgElement.getBackground()).getColor();
                if (color == Color.RED) {
                    bgElement.setBackgroundColor(Color.BLUE);
                }
                else {
                    bgElement.setBackgroundColor(Color.RED);
                }
            }
        });
    }
}
Mohammed Gamal
  • 234
  • 1
  • 8
0

Replace
RelativeLayout bgElement =(RelativeLayout)findViewById(R.layout.activity_main); by RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.activity_main);

and also make sure that the id of the Relative layout is activity_main

0

Another even simpler way to deal with this is by using the layout's XML code. Simply add the following attribute to your tag:

android:background="#ff0000"

Note that the color above is for red, you can modify the RGB values however you like.

4u53r
  • 717
  • 1
  • 7
  • 17