我使用单选按钮进行选择。 当我把setOnCheckedChangeListener放到应用程序崩溃时。 请帮忙。
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();
}
});
有我的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
...
答案 0 :(得分:5)
您需要撤消此
RadioGroup radioReseau = (RadioGroup) findViewById(R.id.radioReseau);
setContentView(R.layout.set_reseau_setting);
所以改为
setContentView(R.layout.set_reseau_setting);
RadioGroup radioReseau = (RadioGroup) findViewById(R.id.radioReseau);
首先需要对布局进行膨胀,然后初始化视图,因为findViewById
会查找当前有效的布局中包含id的视图。