我有三个单选按钮,每个单选按钮都执行一些操作。使用这些单选按钮,我正在过滤我的结果。例如,对于RB1,按日期排序;对于RB2,按客户名称排序;对于RB3,按产品名称排序。
以上两个单选按钮(RB2和RB3)运行良好,但当我选择RB3(按日期排序)时,我的应用程序崩溃了。
final RadioGroup rgroup = (RadioGroup)findViewById(R.id.radioType);
final RadioButton Ta = (RadioButton)findViewById(R.id.type_a);
final RadioButton Tb = (RadioButton)findViewById(R.id.type_b);
final RadioButton Tc = (RadioButton)findViewById(R.id.type_c);
rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (rgroup.getCheckedRadioButtonId() == Ta.getId()) {
cus_names = listItems.get(cus_name.getSelectedItemPosition());
btn1.setVisibility(View.VISIBLE);
btn2.setVisibility(View.VISIBLE);
created_date1.setVisibility(View.VISIBLE);
created_date2.setVisibility(View.VISIBLE);
cus_name.setVisibility(View.GONE);
po_id.setVisibility(View.GONE);
}
else if (rgroup.getCheckedRadioButtonId() == Tb.getId()) {
cus_name.setSelection(0);
cus_name.setVisibility(View.VISIBLE);
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.GONE);
created_date1.setVisibility(View.GONE);
created_date2.setVisibility(View.GONE);
po_id.setVisibility(View.GONE);
}
else if (rgroup.getCheckedRadioButtonId() == Tc.getId()) {
po_id.setSelection(0);
po_id.setVisibility(View.VISIBLE);
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.GONE);
created_date1.setVisibility(View.GONE);
created_date2.setVisibility(View.GONE);
cus_name.setVisibility(View.GONE);
}
}
});
我的日志:
07-28 11:47:53.243 13244-13244/com.example.Minal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.Minal, PID: 13244
java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
at java.util.ArrayList.get(ArrayList.java:310)
at com.example.Minal.Purchase_Pending$1.onCheckedChanged(Purchase_Pending.java:180)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:173)
at android.widget.RadioGroup.-wrap0(RadioGroup.java)
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:351)
at android.widget.CompoundButton.setChecked(CompoundButton.java:159)
at android.widget.CompoundButton.toggle(CompoundButton.java:115)
at android.widget.RadioButton.toggle(RadioButton.java:76)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
cus_name.getSelectedItemPosition()
返回了负数1,而且你的列表为空。有两个问题。 - OneCricketeer