我是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() 给出的 pointerid。
有任何想法吗?