I'm "joking" with the canvas (android) and I'm having trouble removing element from an ArrayList.
The app works like this: The user clicks on the screen, a ball appears that goes up but I want to remove only the balls that have exceeded the limit of y <= -50
. When I go to remove it it generates this error:
09-08 23:28:12.617: E/AndroidRuntime(6719): FATAL EXCEPTION: Thread-2330
09-08 23:28:12.617: E/AndroidRuntime(6719): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
09-08 23:28:12.617: E/AndroidRuntime(6719): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
09-08 23:28:12.617: E/AndroidRuntime(6719): at java.util.ArrayList.get(ArrayList.java:304)
09-08 23:28:12.617: E/AndroidRuntime(6719): at com.dotjogoqualquer.JogoView.update(JogoView.java:50)
09-08 23:28:12.617: E/AndroidRuntime(6719): at com.dotjogoqualquer.JogoView.run(JogoView.java:40)
09-08 23:28:12.617: E/AndroidRuntime(6719): at java.lang.Thread.run(Thread.java:856)
Code
public void update() {
if (obj.size() >= 1){
for(int x = 0; x < obj.size(); x++) {
obj.get(x).y -= 15;
if (obj.get(x).y < -50) {
obj.remove(x); //Erro ocorre aqui
}
}
}
postInvalidate();
}
Links to the complete code link (Line 48)