问题
I'm new to android..
I'm facing a problem with radio buttons..Please guide me.
I have a radio group. First time it displays its unchecked. when user select the option at first time, i didn't face any problem. In mean time i have a next button. when user click on next button the options are unchecked for second question. Like the way user goes by giving next for next question. It works fine up to 4 or 5 questions after that while going for next question it throws error. please guide me.... what im doing wrong?
Thanks a lot in advance.
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"); } });
Logcat
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)
回答1:
Maybe you have IndexArrayOfBounds
in this line
answ1.get((k*4)+0)
because first you increase k
and the use and arrays starts from 0
.
来源:https://stackoverflow.com/questions/14869783/problems-in-radiobuttons