我试图从另一个类调用onTouch方法,但是我不知道要为MotionEvent传入什么。使用null会强制关闭应用程序。下面是代码
public final class Touch {
private static final String TAG = Touch.class.getSimpleName();
public boolean onTouchEvent(MotionEvent event, Droid droid, Thread thread) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
droid.handleActionDown((int)event.getX(), (int)event.getY());
/*if (event.getY() > getHeight() - 50)
{
thread.setRunning(false);
((Activity)getContext()).finish();
}
else
{
Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
}*/
}
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
// the gestures
if (droid.isTouched())
{
// the droid was picked up and is being dragged
droid.setX((int)event.getX());
droid.setY((int)event.getY());
}
}
if (event.getAction() == MotionEvent.ACTION_UP)
{
// touch was released
if (droid.isTouched())
{
droid.setTouched(false);
}
}
return true;
}
}这就是类,我该如何在主线程中调用它呢?目前我正在尝试:
public void Touch()
{
MotionEvent event = null;
touch.onTouchEvent(event, droid, thread);
}因为null是我唯一能想到要传入的东西。
发布于 2012-10-16 22:47:46
尝试:
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);https://stackoverflow.com/questions/12917143
复制相似问题