2013-07-19 83 views
1

我是新來的android開發,所以忍受着我。Android /處理MotionEvent getX()引發異常

我寫了一個處理腳本,可以同時使用兩臺印刷機。爲此,我使用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:

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) 

輸出只是異常之前是:

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

當第二異常被產生的第一指針升空之前

pointercount: 1 
i: 0 
pointerid: 1 

所以有一個指針的指針id是1.顯然getX()引發異常,但我不明白爲什麼,因爲我是usin g由event.getPointerId()給出的指針id。

任何想法?

回答

0

我想通了。

getX()在事件列表中獲取一個索引。不是一個指針。我誤解了它說「使用getPointerId(int)找到這個索引的指針標識符」的文檔。如果您還沒有索引,請不要使用getPointerId爲getX()使用findPointerIndex()生成參數。

更正上面的代碼會...

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

...因爲我的數組被隱式指針ID索引。我知道,有一點哈克。自那時以來,代碼已經發展。