0

我是安卓新手。。

我遇到了单选按钮的问题..请指导我。

我有一个广播组。第一次显示未选中。当用户第一次选择该选项时,我没有遇到任何问题。与此同时,我有一个下一步按钮。当用户单击下一步按钮时,第二个问题的选项未选中。就像用户给出下一个问题的方式一样。之后它可以正常工作多达 4 或 5 个问题,而在处理下一个问题时会引发错误。请指导我....我做错了什么?

提前非常感谢。

    btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) { 
                switch(btn_practicerg.getCheckedRadioButtonId()){
                case R.id.RB1:
                    if (btn_practice1.isChecked()){
                        btn_practice2.setChecked(false);
                        btn_practice3.setChecked(false);
                        btn_practice4.setChecked(false);
                    }
                    break;
                case R.id.RB2:
                    if (btn_practice2.isChecked()){
                        btn_practice1.setChecked(false);
                        btn_practice3.setChecked(false);
                        btn_practice4.setChecked(false);
                    }
                    break;
                case R.id.RB3:
                    if (btn_practice3.isChecked()){
                        btn_practice1.setChecked(false);
                        btn_practice2.setChecked(false);
                        btn_practice4.setChecked(false);
                    }
                    break;
                case R.id.RB4:
                    if (btn_practice4.isChecked()){
                        btn_practice1.setChecked(false);
                        btn_practice2.setChecked(false);
                        btn_practice3.setChecked(false);
                    }
                    break;
                }
                RadioButton radioButton = (RadioButton)group. findViewById(checkedId);
                String temp = radioButton.getText().toString();
                crrtans=new ArrayList<String>(new ArrayList<String>(crrtans));
                if (temp.equals(crrtans.get(l))){
                TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                txtRadio.setText("" + radioButton.getText() + " IS CORRECT");
                       }else{
                TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
            txtRadio.setText("" + radioButton.getText() + "is INCORRECT");
                     }
     });
    Button nextBtn = (Button) findViewById(R.id.nxt_btn);
    nextBtn.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View v){ 
         TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
         txtRadio.setText("Nothing is picked");
         txtRadio.setTextColor(Color.parseColor("#CCEEFF"));
          if (j == ques1.size() -1) {
                showAlert1();
            }
          else{ 
            ++j;
            ++num;
            TextView txtque = (TextView) findViewById(R.id.que_txt); 
            txtque.setText("Q" + num +ques1.get(j));
            btn_practice1.setChecked(false);
             btn_practice2.setChecked(false);
             btn_practice3.setChecked(false);
             btn_practice4.setChecked(false);
            ++k;
            btn_practice1.setText(answ1.get((k*4)+0));
            btn_practice2.setText(answ1.get((k*4)+1));
            btn_practice3.setText(answ1.get((k*4)+2));
            btn_practice4.setText(answ1.get((k*4)+3));
          }
         // 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) { 
                    int selectedId = group.getCheckedRadioButtonId();
                    RadioButton radioButton = (RadioButton)group. findViewById(checkedId);
                    String temp = radioButton.getText().toString();
                    switch(btn_practicerg.getCheckedRadioButtonId()){
                    case R.id.RB1:
                        if (btn_practice1.isChecked()){
                            btn_practice2.setChecked(false);
                            btn_practice3.setChecked(false);
                            btn_practice4.setChecked(false);
                        }
                        break;
                    case R.id.RB2:
                        if (btn_practice2.isChecked()){
                            btn_practice1.setChecked(false);
                            btn_practice3.setChecked(false);
                            btn_practice4.setChecked(false);
                        }
                        break;
                    case R.id.RB3:
                        if (btn_practice3.isChecked()){
                            btn_practice1.setChecked(false);
                            btn_practice2.setChecked(false);
                            btn_practice4.setChecked(false);
                        }
                        break;
                    case R.id.RB4:
                        if (btn_practice4.isChecked()){
                            btn_practice1.setChecked(false);
                            btn_practice2.setChecked(false);
                            btn_practice3.setChecked(false);
                        }
                        break;
                    }
                    l++;
        if (temp.equals(crrtans.get(l))){
        TextView txtRadio = (TextView) findViewById(R.id.rdtxt); // line 407 
        txtRadio.setText("" + radioButton.getText() + " IS CORRECT");
                    }else{
            TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
        txtRadio.setText("" + radioButton.getText() + " IS INCORRECT");
                   }      
         });

日志猫

       E/AndroidRuntime(597): FATAL EXCEPTION: main
       E/AndroidRuntime(597): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
       E/AndroidRuntime(597):   at 
     java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
       E/AndroidRuntime(597):   at java.util.ArrayList.get(ArrayList.java:311)
       E/AndroidRuntime(597):   at   
     com.example.finalpractice.Question$LoadQuestions$2$1.onCheckedChanged(Question.java:407)
       E/AndroidRuntime(597):   at 
     android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
       E/AndroidRuntime(597):   at android.widget.RadioGroup.access$600(RadioGroup.java:52)
       E/AndroidRuntime(597):   at 
     android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
       E/AndroidRuntime(597):   at 
     android.widget.CompoundButton.setChecked(CompoundButton.java:127)
       E/AndroidRuntime(597):   at 
     android.widget.CompoundButton.toggle(CompoundButton.java:86)
       E/AndroidRuntime(597):   at android.widget.RadioButton.toggle(RadioButton.java:72)
       E/AndroidRuntime(597):   at 
     android.widget.CompoundButton.performClick(CompoundButton.java:98)
       E/AndroidRuntime(597):   at android.view.View$PerformClick.run(View.java:9080)
       E/AndroidRuntime(597):   at android.os.Handler.handleCallback(Handler.java:587)
       E/AndroidRuntime(597):   at android.os.Handler.dispatchMessage(Handler.java:92)
       E/AndroidRuntime(597):   at android.os.Looper.loop(Looper.java:123)
       E/AndroidRuntime(597):   at android.app.ActivityThread.main(ActivityThread.java:3683)
       E/AndroidRuntime(597):   at java.lang.reflect.Method.invokeNative(Native Method)
       E/AndroidRuntime(597):   at java.lang.reflect.Method.invoke(Method.java:507)
       E/AndroidRuntime(597):   at 
     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
       E/AndroidRuntime(597):   at 
     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
       E/AndroidRuntime(597):   at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

也许你IndexArrayOfBounds在这一行

answ1.get((k*4)+0)

因为首先你增加k并且使用和数组从0.

于 2013-02-14T07:31:45.950 回答