I have one simple code which implements RadioGroupButton and CheckBox.
When the app was started on emulator, no exception, but when i clicked the radio button, it threw the exception and i noticed from LogCat, there was this NullPointerException caused by calling getId() on a null object reference.
I have reviewed my code and i was lost why the exception came. Here is my code, please kindly help, thanks a lot!
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private RadioGroup GenderGroup;
private RadioButton rbMale;
private RadioButton rbFemale;
private CheckBox cbFootball;
private CheckBox cbBasketball;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GenderGroup = (RadioGroup)super.findViewById(R.id.gender);
rbMale = (RadioButton)super.findViewById(R.id.male);
rbMale = (RadioButton)super.findViewById(R.id.female);
//System.out.printf("rbMale = %@", rbMale);
//System.out.printf("rbFemale = %@", rbFemale);
cbFootball = (CheckBox)super.findViewById(R.id.football);
cbBasketball = (CheckBox)super.findViewById(R.id.basketball);
// register OnCheckedChangeListener event:
GenderGroup.setOnCheckedChangeListener(new GenderOnCheckedChangeListener());
cbFootball.setOnCheckedChangeListener(new FootballOnCheckedChangeListener());
cbBasketball.setOnCheckedChangeListener(new BasketballOnCheckedChangeListener());
}
private class GenderOnCheckedChangeListener implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String sGender = "";
if (checkedId == rbFemale.getId()) {
sGender = rbFemale.getText().toString();
}
if (checkedId == rbMale.getId()) {
sGender = rbMale.getText().toString();
}
Toast.makeText(getApplicationContext(), "Your gender is :" + sGender, Toast.LENGTH_SHORT).show();
}
}
private class FootballOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
String sFav = "";
if (cbFootball.isChecked()) {
sFav = cbFootball.getText().toString();
sFav = sFav + "selected!";
}
else {
sFav = sFav + "not selected!";
}
Toast.makeText(getApplicationContext(), "Your favourite is: " + sFav, Toast.LENGTH_SHORT).show();
}
}
private class BasketballOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
String sFav = "";
if (cbBasketball.isChecked()) {
sFav = cbBasketball.getText().toString();
sFav = sFav + "selected!";
}
else {
sFav = sFav + "not selected!";
}
Toast.makeText(getApplicationContext(), "Your favourite is: " + sFav, Toast.LENGTH_SHORT).show();
}
}
}
Sorry i am still fresh man here, can't paste pictures.
You didn't initialize
rbFemale
. Probably a copy-paste error. Use this: