【问题标题】:MultiTouchEvent Error?多点触控事件错误?
【发布时间】:2012-07-04 15:32:53
【问题描述】:

遇到一些错误,不知道该怎么办,请帮忙。

The method getX() in the type MotionEvent is not applicable for the arguments (int) 
The method getY() in the type MotionEvent is not applicable for the arguments (int) 
The method getX() in the type MotionEvent is not applicable for the arguments (int) 
The method getY() in the type MotionEvent is not applicable for the arguments (int) 
The method getPointerCount() is undefined for the type MotionEvent  
The method getPointerId(int) is undefined for the type MotionEvent  
ACTION_POINTER_DOWN cannot be resolved or is not a field    
The method getX() in the type MotionEvent is not applicable for the arguments (int) 
The method getY() in the type MotionEvent is not applicable for the arguments (int) 
ACTION_POINTER_UP cannot be resolved or is not a field  MultiTouchHandler.java  
ACTION_MASK cannot be resolved or is not a field    
ACTION_POINTER_ID_MASK cannot be resolved or is not a field 
ACTION_POINTER_ID_SHIFT cannot be resolved or is not a field
The method getPointerId(int) is undefined for the type MotionEvent

代码:

public boolean onTouch(View v, MotionEvent event) {  
    synchronized (this) {  
        int action = event.getAction() & MotionEvent.ACTION_MASK;  
        int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >>   MotionEvent.ACTION_POINTER_ID_SHIFT;  
        int pointerId = event.getPointerId(pointerIndex);   

        switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            touchEvent = touchEventPool.newObject();
            touchEvent.type = TouchEvent.TOUCH_DOWN;
            touchEvent.pointer = pointerId;
            touchEvent.x = touchX[pointerId] = (int) (event
                    .getX(pointerIndex) * scaleX);
            touchEvent.y = touchY[pointerId] = (int) (event
                    .getY(pointerIndex) * scaleY);
            isTouched[pointerId] = true;
            touchEventsBuffer.add(touchEvent);
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:
            touchEvent = touchEventPool.newObject();
            touchEvent.type = TouchEvent.TOUCH_UP;
            touchEvent.pointer = pointerId;
            touchEvent.x = touchX[pointerId] = (int) (event
                    .getX(pointerIndex) * scaleX);
            touchEvent.y = touchY[pointerId] = (int) (event
                    .getY(pointerIndex) * scaleY);
            isTouched[pointerId] = false;
            touchEventsBuffer.add(touchEvent);
            break;

        case MotionEvent.ACTION_MOVE:
            int pointerCount = event.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                pointerIndex = i;
                pointerId = event.getPointerId(pointerIndex);

                touchEvent = touchEventPool.newObject();
                touchEvent.type = TouchEvent.TOUCH_DRAGGED;
                touchEvent.pointer = pointerId;
                touchEvent.x = touchX[pointerId] = (int) (event
                        .getX(pointerIndex) * scaleX);
                touchEvent.y = touchY[pointerId] = (int) (event
                        .getY(pointerIndex) * scaleY);
                touchEventsBuffer.add(touchEvent);
            }
            break;
        }

        return true;
    }
}

【问题讨论】:

  • 你确定你导入了正确的MotionEvent吗?你需要android.view.MotionEvent
  • 是的,我希望我能显示我所有的代码

标签: java android


【解决方案1】:

读取第一个错误:

The method getX() in the type MotionEvent is not applicable for the arguments (int) 

这表示 MotionEvent 上的 getX() 不能使用 int 作为参数调用,即 getX(1)

在你的代码中我可以看到:

touchEvent.x = touchX[pointerId] = (int) (event.getX(pointerIndex) * scaleX);

如果您看到您正在调用 getX(pointerIndex)。这是一个编译错误。

如果您阅读其他错误并查看您的代码,您应该能够找到所有错误。

我的建议:获取一个 IDE(请参阅 Which Eclipse version should I use for an Android app? 以帮助选择),所有这些问题都会以红色突出显示。

【讨论】:

  • 好吧,我正在尝试使用 api level 3 并使用最新版本的 eclipse 进行开发
猜你喜欢
  • 2011-03-16
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多