I have a adapter which extends BaseAdapter in my activity. In adapter I list the records. When the user click any record, options are listed. These options are "send" and "delete". Delete options remove record from list and send option send record to server. When the send is pressed , asynctask is started. For the delete operation, I use handler in the dialog.(Yes or no).
My problem is that if I press delete in one record while other records are sending, application is crashed and I got this excetion;
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
E/AndroidRuntime(25140): atandroid.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
I checked the size many times and size is not 0. I am sure that size is not problem.
This code is from my delete operation;
public class MyAdapter extends BaseAdapter{
CopyOnWriteArratList<MyObject> MyCopyonWriteArrayList;
private Handler handler;
private ListView listview;
public MyAdapter(Context context,
CopyOnWriteArratList<MyObject> MyCopyonWriteArrayList,ListView listView){
this.context = context;
this.MyCopyonWriteArrayList= MyCopyonWriteArrayList;
this.listView = listView;
}
new Thread(new Runnable() {
@Override
public void run() {
//If I remove this line, app does not crash
MyObject o = MyCopyonWriteArrayList.get(position);
o.Delete();
MyCopyonWriteArrayList.remove(position);
//Code arrive here.
}
handler.post(new Runnable() {
//Code cannot arrive here
@Override
public void run() {
progressDialog.dismiss();
deleteDoneListener.deleteDone();
}
});
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
My asyntask is very typical.
My question is that why I got this exception " index 0 , size 0"?. This size is not myarraylist size.
How can I fix this code and what is the source of this size?
Also If I use asynctask instead of handler in the delete, I got "Can't create handler inside thread that has not called Looper.prepare()]" exception. So I cannot put asyntack in the dialog.