AndroidBoy AndroidBoy - 4 months ago 18
Android Question

When I select radio button my app crashed

I have 3 Radio buttons each radio buttons to do some performs, Using this Radio Buttons I am filtering my Results E.g for RB1 for DATE-WISE RB2 for customernames and RB3 for Productname.

Above two radio buttons working good(RB2 and RB3), but when I choose RB3(DATE-WISE) my Application Crashed.

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


My Logcat:

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)

Answer

You have written below line at wrong position, so whenever you will get empty list it will crash your app,

cus_names = listItems.get(cus_name.getSelectedItemPosition());

So try below code for all three condition not in onle,

if(listItmes != null & listItems.size() > 0)
      cus_names = listItems.get(cus_name.getSelectedItemPosition());
Comments