I'm adding/removing Items to/from ArrayLists in my app in the following way:
public long gotoItem(int pos) {
if (pos > 0) {
List<QueueItem> mSubList = mItems.subList(0, pos);
switch (mRepeatType) {
case NO_REPEAT:
case REPEAT_SINGLE:
mPlayedItems.addAll(mSubList);
mItems.removeAll(mSubList);
break;
case REPEAT_ALL:
mItems.removeAll(mSubList);
mItems.addAll(mSubList);
break;
}
}
return (long) mItems.get(0).getItem();
}
I get the following exception thrown:
> java.util.ConcurrentModificationException at java.util.AbstractList$SubAbstractList.listIterator(AbstractList.java:314)
at java.util.AbstractList$SubAbstractList.iterator(AbstractList.java:301)
at java.util.AbstractCollection.contains(AbstractCollection.java:127)
at java.util.AbstractCollection.removeAll(AbstractCollection.java:277)
at at.guger.musixs.playback.queue.Queue.gotoItem(Queue.java:135)
at at.guger.musixs.playback.MusicService.gotoPosition(MusicService.java:500)
at at.guger.musixs.playback.MusicPlayer.gotoPosition(MusicPlayer.java:138)
at at.guger.musixs.activity.MainActivity.SyncLocation(MainActivity.java:707)
at at.guger.musixs.activity.MainActivity.access$200(MainActivity.java:73)
at at.guger.musixs.activity.MainActivity$1.onServiceConnected(MainActivity.java:605)
at at.guger.musixs.playback.MusicPlayer$ServiceBinder.onServiceConnected(MusicPlayer.java:246)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1224)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1241)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Why does this error occur here and how can I fix this error in the simplest way?? - Do I really have to make a loop removing (and adding) the items from/to the arraylists?
Thank you!