So I was following along with a 2D graphics tutorial (those interested, its the one @ http://droidnova.com/), and I got to a point where I’ve been experiencing constant crashes in my onTouchEvent() callback. I’m still trying to track the bug, but while trying to debug the issue I noticed that my onTouchEvent callback gets called twice for each event.
For rerference
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
synchronized (getHolder()) {
if (action == MotionEvent.ACTION_DOWN) {
if (gameObjects.size() < 2) {
currentlySelected = new GameObject(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
currentlySelected.getCoordinates().setX((int) event.getX());
currentlySelected.getCoordinates().setY((int) event.getY());
} else {
for (GameObject gameObject: gameObjects) {
if (gameObject.inBoundingBox(event.getX(), event.getY())) {
currentlySelected = gameObject;
break;
}
}
if (currentlySelected != null) {
// Remember to remove it from the list so that we don't waste time updating it in updatePhysics twice.
gameObjects.remove(currentlySelected);
}
}
} else if (action == MotionEvent.ACTION_UP && (currentlySelected != null)) {
gameObjects.add(currentlySelected);
currentlySelected = null;
} else if (action == MotionEvent.ACTION_MOVE) {
currentlySelected.getCoordinates().setX((int) event.getX());
currentlySelected.getCoordinates().setY((int) event.getY());
}
}
No when I put a breakpoint on the first line, and run it in debug mode, when I touch the screen the callback gets called with the following action’s…
- action = 0 (MotionEvent.ACTION_DOWN)
- action = 0 (MotionEvent.ACTION_DOWN)
- action = 1 (MotionEvent.ACTION_MOVE)
- action = 1 (MotionEvent.ACTION_MOVE)
- action = 2 (MotionEvent.ACTION_UP)
- action = 2 (MotionEvent.ACTION_UP)
for each action, the second one gets out at the synchronized call. Anyone know the reason for this behavior?
As it turns out, the problem was with the line
It should really be
That seemed to solve it for me.