RadioGroup中单选按钮的SharedPreferences.Editor

时间:2013-06-03 10:10:37

标签: android radio-button sharedpreferences

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

我想在用户进入下一个时检查用户选择的选项,然后再次来到上一个。

我试过这样就抛出了错误。干嘛做错了什么?

提前多多感谢。

     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)

1 个答案:

答案 0 :(得分: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);
  });

试试这种方式。我希望这会对你有所帮助。