7

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

kenneho
  • 401
  • 1
  • 7
  • 24
  • You're trying to access the ArrayList when it's empty. Post some code. – VERT9x Mar 12 '15 at 17:07
  • Can you post your activity code? size is 0 means your list is empty. – Sudheesh Mohan Mar 12 '15 at 17:10
  • *The removal is done inside the getView method.* and here is the problem also remove method from adapter is already calling notifyDataSetChanged ... – Selvin Mar 12 '15 at 17:17
  • You should post the LogCat too since there are useful Log calls that the code made. If you step thru code via debugger, then you should know which code causes the Exception. – The Original Android Mar 12 '15 at 17:37
  • @Selvin, how do you suggest I put the code that removes entries from the adapter? – kenneho Mar 12 '15 at 17:44
  • Do not create new Adapter inside doInBackground(and new array list) ... just modify the old one (based on results - so here you should put the code which is cheching if you need to remove item) and call notify from onPostExecute ... – Selvin Mar 12 '15 at 17:53
  • That makes sense. I'll modify my code accordingly within the next few days, and see if that eliminates the bug. – kenneho Mar 12 '15 at 20:16
  • I refactored my code so that entries are removed outside of the adapter itself, but still I get the IndexOutOfBoundsException. I the latest logcat (http://pastebin.com/13kQm74q) I found an interesting thing, though, after my phone has been inactive throughout the night - the Runnable execution, which are ran with one second intervals, are suddenly comming in a big burst. Notice the timestamp at http://pastebin.com/13kQm74q. Any ideas as to why the Runnable thread suddenly fires off in a burst? – kenneho Mar 14 '15 at 06:23
  • Btw, I just added "timerHandler.removeCallbacks(timerRunnable);" to onPause(), and "timerHandler.postDelayed(timerRunnable, 1000);" to onResume(). I'm guessing that the Runnable might be (part of) the bug. – kenneho Mar 14 '15 at 06:45

1 Answers1

1

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.

kenneho
  • 401
  • 1
  • 7
  • 24