4

I have created a activity according to RadioButton that user choose. i didn't know why my code was not working. this is my non working code.(i want to change background color when user checked radiobutton)

public class RegisterActivity  extends Activity {   

/** Called when the activity is first created. */
RadioButton rbRed;
RadioButton rbBlue;
TexView header;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.cafechoice);

    rbRed = (RadioButton) findViewById(R.id.radioRed);
    rbBlue = (RadioButton) findViewById(R.id.radioBlue);
    header = (TextView) findViewById(R.id.textview);

    rbRed.setOnCheckChangeListener(radio_listener);
    rbBlue.setOnCheckChangeListener(radio_listener);

}
final CompoundButton.OnCheckedChangeListener radio_listener = new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
    if (!isChecked)
        return;
    if (buttonView.getId() == R.id.radioRed)
    {
    header.setBackgroundColor(Color.parseColor("#E23DA4"));
    }
    else if (buttonView.getId() == R.id.radioBlue)
    {
    header.setBackgroundColor(Color.parseColor("#548BF7"));
    }
}
};

logcat showed up error on

rbRed.setOnCheckChangeListener(radio_listener);

caused by java.lang.nullpointerexception

Indra
  • 79
  • 1
  • 1
  • 6

0 Answers0