Android / Processing MotionEvent getX()引发异常

时间:2013-07-19 10:55:57

标签: android exception processing motionevent

我是Android开发的新手,所以请耐心等待。

我编写了一个处理脚本,可以同时使用两个按钮。要做到这一点,我正在使用android.view.motionevent。在我的主要脚本(pde)中我有这个:

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

这很好用。问题在于处理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);
    }
  }

当第一个指针在 之前时,第二个指针会产生一个异常:

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)

异常之前的输出是:

pointercount: 1
i: 0
pointerid: 1

所以有一个指针的指针ID为1.显然,getX()正在引发异常,但我无法理解为什么,因为我正在使用event.getPointerId()给出的指针。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我明白了。

getX()获取事件列表中的索引。不是指针。我误解了文档里面说“使用getPointerId(int)来查找这个索引的指针标识符”;如果你还没有索引,请不要使用getPointerId为getX()生成参数使用findPointerIndex()。

对上述代码的更正将是......

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

...因为我的数组是由指针id隐式索引的。我知道有点hacky。从那以后,代码一直在继续。