如何使用廣播組中單選按鈕的共享首選項。我有兩個按鈕(下一個和上一個)。RadioGroup中RadioButton的SharedPreferences.Editor
我想檢查用戶選擇的選項,當用戶轉到下一個,並再次來到上一次。
我試過這樣會引發錯誤。在做什麼錯誤?
非常感謝。
ImageView nextBtn = (ImageView) findViewById(R.id.nxt_btn); nextBtn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ btn_practice1.setChecked(false); btn_practice2.setChecked(false); btn_practice3.setChecked(false); btn_practice4.setChecked(false); btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton)group. findViewById(checkedId); int checkedIndex = group.indexOfChild(radioButton); getSharedPreferences(KEY_SAVED_RADIO_BUTTON_INDEX, checkedIndex); SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("check", checkedIndex); editor.commit(); }); }); ImageView previousbtn1 = (ImageView) findViewById(R.id.prv_btn); previousbtn1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0); RadioButton savedCheckedRadioButton = (RadioButton)group.getChildAt(savedRadioIndex); // line number 570 savedCheckedRadioButton.setChecked(true); });
logcat的
E/AndroidRuntime(362): FATAL EXCEPTION: main E/AndroidRuntime(362): java.lang.NullPointerException E/AndroidRuntime(362): at com.example.TEENEINSTIEN.Question$LoadQuestions$4.onClick(Question.java:570) E/AndroidRuntime(362): at android.view.View.performClick(View.java:2485) E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:9080) E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587) E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92) E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:3683) E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:507) E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method)
哪行代碼顯示錯誤。 –
我在我的代碼行號570上面提到了 –
但我們不能計算你的行號。所以顯示哪個是570行號碼 – Nirali