1

I'm new to android development so bear with me.

I've written a processing script that works with two simultaneous presses. To do this I'm using android.view.motionevent. In my main script (pde) I have this:

public boolean surfaceTouchEvent(MotionEvent event) {
  if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
  //do action down stuff...
  }
  //etc do other actions...

This works fine. The problem comes in handling the ACTION_MOVE:

  //ACTION_MOVE
  else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
    int pointercount = event.getPointerCount();
    print("pointercount: " + str(pointercount));
    for (int i = 0; i<pointercount; i++) {
      int pointerId = event.getPointerId(i);
      print("i: " + str(i));
      print("pointerid: " + str(pointerId));
      movex[pointerid] = event.getX(pointerId);
      movey[pointerid] = event.getY(pointerId);
    }
  }

When the first pointer lifts off before the second an exception is produced:

FATAL EXCEPTION: main
java.lang.IllegalArgumentException: pointerIndex out of range
    at android.view.MotionEvent.nativeGetAxisValue(Native Method)
    at android.view.MotionEvent.getX(MotionEvent.java:1974)
    at processing.test.scrapeashape.ScrapeAShape.surfaceTouchEvent(ScrapeAShape.java:131)
    at processing.core.PApplet$SketchSurfaceView.onTouchEvent(Unknown Source)
    at android.view.View.dispatchTouchEvent(View.java:5604)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2060)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1829)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2060)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1829)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2060)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1829)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1917)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1376)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1865)
    at android.view.View.dispatchPointerEvent(View.java:5784)
    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2894)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2470)
    at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2479)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4448)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
    at dalvik.system.NativeStart.main(Native Method)

The output just before the exception is:

pointercount: 1
i: 0
pointerid: 1

So there is one pointer who's pointer id is 1. Clearly the getX() is raising the exception but I can't understand why, since i'm using the pointerid given by event.getPointerId().

Any ideas?

powlo
  • 2,538
  • 3
  • 28
  • 38

1 Answers1

0

I figured it out.

getX() takes an index in the event list. Not a pointerid. I misinterpreted the documentation where it says "use getPointerId(int) to find the pointer identifier for this index"; don't use getPointerId to generate a parameter for getX() use findPointerIndex() if you don't already have the index.

Correction to the code above would be...

  movex[pointerid] = event.getX(i);
  movey[pointerid] = event.getY(i);

...because my array is implicitly indexed by pointer id. Slightly hacky I know. Code has moved on since then.

powlo
  • 2,538
  • 3
  • 28
  • 38