I keep getting this IndexOutOfBoundsException, but can't seem to figure out what's causing it. My listview has an adapter with a list of objects, and the objects are removed based on a timestamp. The removal is done inside the getView method. Once an item is removed, I call notifyDataSetChanged().
The full source code is available on github, and here's 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 I keep getting:
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 within getView can get as high as six or seven.
Can anyone here spot the bug? Any help will be appreciated.
Regards, Kenneth
EDIT 1: * Link to the activity code that uses the : https://github.com/kenneho/run-for-the-bus/blob/master/app/src/main/java/net/kenneho/runnow/InfoActivity.java * I've pasted the most relevant logcat part here: http://pastebin.com/5FtU4EaM
Finally the bug is fixed. First, I refactored the code as suggested by @Selvin. Second, I added a "removecallback"-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 tracking down this bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With