I created a little android game but I am getting weird error.
Here is log:
12-31 16:10:22.407: E/AndroidRuntime(12824): FATAL EXCEPTION: Thread-1461
12-31 16:10:22.407: E/AndroidRuntime(12824): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
12-31 16:10:22.407: E/AndroidRuntime(12824): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
12-31 16:10:22.407: E/AndroidRuntime(12824): at java.util.ArrayList.get(ArrayList.java:304)
12-31 16:10:22.407: E/AndroidRuntime(12824): at **.***.****.GameScreen.updatePaused(GameScreen.java:91)
12-31 16:10:22.407: E/AndroidRuntime(12824): at **.***.****.GameScreen.update(GameScreen.java:43)
12-31 16:10:22.407: E/AndroidRuntime(12824): at **.***.****.framework.impl.AndroidFastRenderView.run(AndroidFastRenderView.java:39)
12-31 16:10:22.407: E/AndroidRuntime(12824): at java.lang.Thread.run(Thread.java:856)
And here is part of code executed :
int len = touchEvents.size();
for(int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i); ->>> this line
.
.
.
What is wrong ?
If you are having threading issues you’ll have to either synchronize on the collection prior to looping like this
Or, you can make a local copy of the collection and loop over that, if it’s still valid to process events in the collection if they have been removed while you’re working on the copy.