I don’t know if this is a simple problem or not, but I just can’t see what the problem is. I’ve gotten three reports now from my app in Google Play of an IndexOutOfBoundsException at points.get(++i).
public GameThread(SurfaceHolder sHolder, Context context, Handler handler)
{
points = new ArrayList<Float>();
running = true;
mSurfaceHolder = sHolder;
}
protected void doDraw(Canvas canvas)
{
Paint p = new Paint();
p.setStyle(Paint.Style.FILL);
p.setColor(Color.WHITE);
for (int i = 0; i < points.size(); i++)
{
float x = points.get(i);
float y = points.get(++i);
canvas.drawPoint(x, y, p);
}
}
public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
points.add(x);
points.add(y);
return true;
}
The index it fails at is trying to access the location equal to its size (Actually one error says which makes NO sense at all to me), and its size varies.
java.lang.IndexOutOfBoundsException: Invalid index 2205, size is 2206
The only way I can see this happening is if for some reason only one object is added to points, and I don’t know why that would happen. onTouchEvent isn’t running in its own thread, is it?
The problem is at this line:
When
i = points.size()-1, you preincrement, and then you invokepoints.get(++i);. At this point,i = points.size()=> Out of bounds. Just remove the++to fix it.EDIT
Alternatively, you can try this approach: