IndexOutOfBoundsException in my Android Listview adapter

I keep getting this IndexOutOfBoundsException, but I can't figure out what causes it. My list has an adapter with a list of objects, and the objects are deleted based on the timestamp. Deletion is performed inside the getView method. Once the item is deleted, I call notifyDataSetChanged ().

The full source code is available on github, and here is a link to the listview adapter code: https://github.com/kenneho/run-for-the-bus/blob/master/app/src/main/java/net/kenneho/runnow /adapters/TravelsAdapter.java

This is the beginning of the stacktrace that I get:

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164) at android.widget.ListView.dispatchDraw(ListView.java:3307) at android.view.View.draw(View.java:15213) <snip> 

I see that often the position value in getView can reach six or seven.

Can anyone spot an error here? Any help would be appreciated.

Regards, Kenneth

EDIT 1: * Link to an activity code that uses: https://github.com/kenneho/run-for-the-bus/blob/master/app/src/main/java/net/kenneho/runnow/InfoActivity. java * I inserted the most appropriate part of logcat here: http://pastebin.com/5FtU4EaM

+7
android indexoutofboundsexception listview
source share
1 answer

Finally, the bug is fixed. First, I reorganized the code suggested by @Selvin. Secondly, I added a "removeecallback" -statement to stop any runnables before creating new ones:

 // Make sure we stop existing timers, if any. timerHandler.removeCallbacks(timerRunnable); timerRunnable = new Runnable() { @Override public void run() { removeExpiredTravels(myAdapter); myAdapter.notifyDataSetChanged(); timerHandler.postDelayed(this, 1000); } }; timerHandler.postDelayed(timerRunnable, 0); 

Thanks for helping me track this error.

+1
source share

All Articles