3

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.

SavasCinar
  • 667
  • 3
  • 8
  • 25
  • see this post http://stackoverflow.com/questions/8431342/listview-random-indexoutofboundsexception-on-froyo – ρяσѕρєя K Dec 21 '12 at 18:50
  • check your arraylist MyCopyonWriteArrayList, use logcat to print the arraylist. – kumar_android Dec 21 '12 at 18:50
  • I checked the arraylist many time and size never 0. – SavasCinar Dec 21 '12 at 18:51
  • What are you trying to do? I think that piece of code is not enough. For example, I would like to know where `MyCopyonWriteArrayList` is defined and where elements are being added to it. – dmon Dec 21 '12 at 19:31
  • I edited question and I defined it in MyAdapter class. I pass the elements to MyAdapter class. Please ask me which part do you need, I will add to question. – SavasCinar Dec 21 '12 at 19:40
  • The stack trace means that the header / footer array in `HeaderViewListAdapter.java` is empty, perhaps you have used `addHeaderView(null)`... – Sam Dec 21 '12 at 21:30

2 Answers2

2

Just check the position lies within the range, if so delete it and make changes as follows :

if(position != -1 && position < MyCopyonWriteArrayList.size()) {
     MyCopyonWriteArrayList.remove(position); 
 }

Definitely it will help you

RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84
  • 1
    I already added these lines. Even size > 0, I got exception. In my question, I already said that this exception is not from MyCopyonWriteArrayList. Code can successfully remove object but when the it arrived handler, exception is occurred. – SavasCinar Dec 21 '12 at 18:58
0

What I was doing, how, and what I've got are almost the same as yours! This could help:

I was getting the same exception while using custom adapter with ListView. And exception was thrown from Android standard library classes, not even leading to any line of my code. I also was adding Header, so my adapter was implicitly wrapped by HeaderViewListAdapter. In my case the problem appears when I'm deleting data from adapter.

I was thinking that the problem is because ListView or adapter can't work fine with Header, but for real the reason was other.

My solution is to make sure that adapter's data is never changed from other threads, and notifyDataSetChanged() is called from UI thread.

So, after fixes everything works and my code looks like:

// Loader's callbacks
@Override
public Loader<...> onCreateLoader(int id, Bundle args) {
    return new Loader...
}

@Override
public void onLoadFinished(Loader<...> loader, Data data) {
    ...
    // adapter's list of items
    listItems.clear();
    listItems.addAll(data);
    adapter.notifyDataSetChanged();
    ...
}

@Override
public void onLoaderReset(Loader<List<? extends Map<String, ?>>> loader) {
    ...
}

// custom loader 
private static class ContactAsyncLoader extends AsyncTaskLoader<...> {
    @Override
    public List<..> loadInBackground() {
        Data data = new ..
        ...
        return data;
    }
}
B-GangsteR
  • 2,534
  • 22
  • 34