0

如何在单选组中使用单选按钮的共享首选项。我有两个按钮(下一个和上一个)。

我想在用户转到下一个并再次回到上一个时检查用户选择的选项。

我试过这样它会抛出错误。做错了什么?

提前非常感谢。

     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);
  });

日志猫

      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)
4

1 回答 1

0

也许group是不可见的或未在您的previousbtn1.setOnClickListener(). 尝试RadioGroup myradioGroup = (RadioGroup) findViewById(R.id.radioGroup1);在您的previousbtn1.setOnClickListener().

 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("check", 0);
       System.out.println("savedRadioIndex is "+savedRadioIndex);
       RadioGroup myradioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
       RadioButton savedCheckedRadioButton = (RadioButton)myradioGroup.getChildAt(savedRadioIndex);
                             // throws error line number 570
       savedCheckedRadioButton.setChecked(true);
  });

试试这个方法。我希望这能帮到您。

于 2013-06-03T10:24:47.283 回答