我不想编写动态壁纸,我需要一个当前按下的点列表。我将点存储在SpareArray中(基本上与HashMap相同,但更快)。
现在,当我用两个手指(1和2)触摸屏幕并抬起手指(第一个手指2,而不是1)时,它可以工作。但是当我首先抬起手指1时,应用程序崩溃了。我必须以相反的顺序抬起手指然后按下它们。
@Override
public void onTouchEvent(MotionEvent event) {
for (int i = 0; i < event.getPointerCount(); i++) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_MOVE:
int pID = event.getPointerId(i);
Points.put(pID, new Point((int)event.getX(pID), (int)event.getY(pID))); //CRASH HERE
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
Points.remove(event.getPointerId(i));
break;
}
}
super.onTouchEvent(event);
handler.post(drawRunnable);
}
我总是得到“java.lang.IllegalArgumentException:pointerIndex超出范围”异常
谢谢,多米尼克
编辑:堆栈跟踪
07-16 19:55:58.354: E/AndroidRuntime(4986): FATAL EXCEPTION: main
07-16 19:55:58.354: E/AndroidRuntime(4986): java.lang.IllegalArgumentException: pointerIndex out of range
07-16 19:55:58.354: E/AndroidRuntime(4986): at android.view.MotionEvent.nativeGetAxisValue(Native Method)
07-16 19:55:58.354: E/AndroidRuntime(4986): at android.view.MotionEvent.getX(MotionEvent.java:1974)
07-16 19:55:58.354: E/AndroidRuntime(4986): at net.ta.livewp.LiveWallpaper$xEngine.onTouchEvent(LiveWallpaper.java:103)
07-16 19:55:58.354: E/AndroidRuntime(4986): at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1047)
07-16 19:55:58.354: E/AndroidRuntime(4986): at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
07-16 19:55:58.354: E/AndroidRuntime(4986): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 19:55:58.354: E/AndroidRuntime(4986): at android.os.Looper.loop(Looper.java:137)
07-16 19:55:58.354: E/AndroidRuntime(4986): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-16 19:55:58.354: E/AndroidRuntime(4986): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 19:55:58.354: E/AndroidRuntime(4986): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 19:55:58.354: E/AndroidRuntime(4986): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-16 19:55:58.354: E/AndroidRuntime(4986): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-16 19:55:58.354: E/AndroidRuntime(4986): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我相信你想要event.getX(i)
而不是event.getX(pID)
。 getPointerId(i)返回一个指针标识符,而不是指针的索引,这就是你想要的。
有关此内容的更多信息,请参阅MotionEvent docs