Android /处理MotionEvent getX()引发异常 [英] Android/Processing MotionEvent getX() raises exception

查看:479
本文介绍了Android /处理MotionEvent getX()引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手,所以请多多包涵。

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

我编写了一个处理脚本,可同时按下两次。为此,我使用的是android.view.motionevent。在我的主脚本(pde)中,我有这个命令:

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...

这很好。问题在于处理ACTION_MOVE:

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);
    }
  }

当第一个指针升起之前 / em>产生第二个异常:

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)

异常之前的输出是:

pointercount: 1
i: 0
pointerid: 1

所以有一个指针的指针ID为1。显然,getX()正在引发异常,但我不明白为什么,因为我使用的是event.getPointerId()给定的指针ID。

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().

任何想法?

推荐答案

我知道了。

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

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);

...因为我的数组由指针ID隐式索引。我知道有点。自那时以来,代码一直在发展。

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

这篇关于Android /处理MotionEvent getX()引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆