Android MotionEvent.getHistoricalX为什么会出现异常

lev*_*gli 2 android exception motionevent

我正在使用a GestureDetector.OnGestureListener来实现Android中的双指缩放.我正在扩展一个TextView类,因此该方法setTextSize()已经实现.以下是我的代码onScroll().

 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
     if(LOGGING) Log.v(MODULE_NAME, "onScroll()");

     float x, y;
     float oldDist, newDist;

     if (e2.getPointerCount() == 2 && 
         ((e2.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_MOVE)) {
         x = e2.getHistoricalX(0, 0) - e2.getHistoricalX(1, 0);
         y = e2.getHistoricalY(0, 0) - e2.getHistoricalY(1, 0);
         oldDist = FloatMath.sqrt(x * x + y * y);
         if (oldDist > 10) {
             if (LOGGING) Log.d(MODULE_NAME, "Starting ZOOM mode");                    
             x = e2.getX(0) - e2.getX(1);
             y = e2.getY(0) - e2.getY(1);
             newDist = FloatMath.sqrt(x * x + y * y);

             if (newDist > 30) {
                    float scaleFactor = (newDist > oldDist) ? oldDist : newDist;
                    float scale = (newDist - oldDist) / scaleFactor;
                    // Callback to be processed in main thread
                    setTextSize(scale * currentTextSize);
                    oldDist = newDist;
             }      
             scrollDetected = true;
             return true;
         }
     }
     return false;            
 }
Run Code Online (Sandbox Code Playgroud)

我的问题是偶然,并非总是,getHistoricalX()抛出异常.我附加了LogCat结果.

07-31 16:53:30.358: V/ZoomTextView(19540): onScroll()
07-31 16:53:30.358: E/InputEventReceiver(19540): Exception dispatching input event.
07-31 16:53:30.358: E/MessageQueue-JNI(19540): Exception in MessageQueue callback: handleReceiveCallback
07-31 16:53:30.358: E/MessageQueue-JNI(19540): java.lang.IllegalArgumentException: historyPos out of range
07-31 16:53:30.358: E/MessageQueue-JNI(19540):  at android.view.MotionEvent.nativeGetAxisValue(Native Method)
07-31 16:53:30.358: E/MessageQueue-JNI(19540):  at android.view.MotionEvent.getHistoricalX(MotionEvent.java:2739)
Run Code Online (Sandbox Code Playgroud)

因为我知道有两个指针并且历史记录的大小至少为0,所以我不清楚这个异常是如何或为何发生的.有人可以帮忙吗?

我看到了这个类似的问题,对我没有任何帮助.

Geo*_*its 8

使用前getHistoricalX()或者getHistoricalY(),你应该检查的大小getHistorySize().

如果它返回零,则表示没有历史事件.然后你只需要使用getX()和处理当前事件getY().