Andrey
Andrey

Reputation: 231

Android Radio Button selection

I use radio button for selection. When i put the setOnCheckedChangeListener the application crach. Please help.

public class SetReseau extends Activity{

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    RadioGroup radioReseau = (RadioGroup) findViewById(R.id.radioReseau);
    setContentView(R.layout.set_reseau_setting);

    radioReseau.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) { 
            RadioButton radioButton = (RadioButton) findViewById(checkedId);
            Toast.makeText(getApplicationContext(), "" + radioButton.getText(), Toast.LENGTH_LONG).show(); 
        }
    });

There is my logcat

E/AndroidRuntime(18822): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(18822): java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.sms/com.sms.SettingsActivity}: 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sms/com.sms.SetReseau}: java.lang.NullPointerException

...

Upvotes: 0

Views: 106

Answers (1)

Raghunandan
Raghunandan

Reputation: 133570

You need to reverse this

 RadioGroup radioReseau = (RadioGroup) findViewById(R.id.radioReseau);
 setContentView(R.layout.set_reseau_setting);

So change to

 setContentView(R.layout.set_reseau_setting); 
 RadioGroup radioReseau = (RadioGroup) findViewById(R.id.radioReseau);

You need to inflate the layout first then initialize the views as findViewById looks for a view with the id in the current infalted layout.

Upvotes: 5

Related Questions