我收到了一個RadioGroup和2個RadioButton。
如果我點擊rb_yes整型url_ask設置爲0。
如果我點擊rb_no整型url_ask設置爲1最後選中的RadioButton在應用程序重啓時停止
我想要的應用程序重新啓動後的最後一次檢查單選按鈕也被選中。
我想這一點,但每次我開始崩潰的應用程序:
//OnCreate
SharedPreferences settings = getSharedPreferences("SETTINGS", Context.MODE_PRIVATE);
super.onCreate(savedInstanceState);
color = settings.getInt("COLOR", 1);
url_ask = settings.getInt("URL_ASK", 0);
if(color == 1) {
setContentView(R.layout.layout_main);
}
if(color == 2) {
setContentView(R.layout.layout_main_red);
}
if(color == 3) {
setContentView(R.layout.layout_main_green);
}
if(color == 4) {
setContentView(R.layout.layout_main_yellow);
}
if(color == 5) {
setContentView(R.layout.layout_main_pink);
}
final RadioButton rb_no = (RadioButton)findViewById(R.id.radioButton_links_no);
final RadioButton rb_yes = (RadioButton)findViewById(R.id.radioButton_links_yes);
if(url_ask == 0) {
rb_yes.setSelected(true);
rb_no.setSelected(false);
}
if(url_ask == 1) {
rb_no.setSelected(true);
rb_yes.setSelected(false);
}
//OnClick for the RadioButtons
public void url_ask_no (View v) {
SharedPreferences settings = getSharedPreferences("SETTINGS", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("URL_ASK", 1);
editor.commit();
url_ask = 1;
}
public void url_ask_yes (View v) {
SharedPreferences settings = getSharedPreferences("SETTINGS", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("URL_ASK", 0);
editor.commit();
url_ask = 0;
}
編輯:
我知道現在我做錯了。我想在另一個佈局上使用findViewByID,而不是在我的layout_main上。這是問題所在。
我沒有看到你在你的'onCreate'中調用setContentView(),所以你的'RadioButton'可能是'null' – 0xDEADC0DE 2014-09-19 12:26:40