我在检测手势时遇到问题。每次双击时我都会收到一个错误,并且在互联网上搜索它的含义似乎是徒劳的——我只能得到不适用于我的特定情况的解决方案。
这是代码:
package com.example.rpgengine;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
public class MainActivity extends Activity {
private ZoneView currentZone;
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentZone = (ZoneView) findViewById(R.id.zoneview);
setContentView(R.layout.activity_main);
// initialize the GestureDetector
gestureDetector = new GestureDetector(this, gestureListener);
}
@Override
public void onPause() {
super.onPause();
} // end method onPause
// release resources
@Override
protected void onDestroy() {
super.onDestroy();
} // end method onDestroy
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// get int representing the type of action which caused this event
int action = event.getAction();
// the user user touched the screen or dragged along the screen
if (action == MotionEvent.ACTION_DOWN
|| action == MotionEvent.ACTION_MOVE) {
//currentZone.drawPath(event);
} // end if
// call the GestureDetector's onTouchEvent method
return gestureDetector.onTouchEvent(event);
} // end method onTouchEvent
// listens forjk touch events sent to the GestureDetector
SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
// called when the user double taps the screen
@Override
public boolean onDoubleTap(MotionEvent e) {
currentZone.movePlayer(e); // fire the cannonball
return true; // the event was handled
} // end method onDoubleTap
};
}
这是错误:
03-28 03:19:54.165: E/InputEventReceiver(5931): Exception dispatching input event.
03-28 03:19:54.165: E/MessageQueue-JNI(5931): Exception in MessageQueue callback: handleReceiveCallback
03-28 03:19:54.165: E/MessageQueue-JNI(5931): java.lang.NullPointerException
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at com.example.rpgengine.MainActivity$1.onDoubleTap(MainActivity.java:62)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:498)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at com.example.rpgengine.MainActivity.onTouchEvent(MainActivity.java:54)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.app.Activity.dispatchTouchEvent(Activity.java:2399)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.view.View.dispatchPointerEvent(View.java:7307)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.os.MessageQueue.nativePollOnce(Native Method)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.os.MessageQueue.next(MessageQueue.java:125)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.os.Looper.loop(Looper.java:124)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 03:19:54.165: E/MessageQueue-JNI(5931): at dalvik.system.NativeStart.main(Native Method)
03-28 03:19:54.165: D/AndroidRuntime(5931): Shutting down VM
03-28 03:19:54.165: W/dalvikvm(5931): threadid=1: thread exiting with uncaught exception (group=0x40e2c300)
03-28 03:19:54.165: E/AndroidRuntime(5931): FATAL EXCEPTION: main
03-28 03:19:54.165: E/AndroidRuntime(5931): java.lang.NullPointerException
03-28 03:19:54.165: E/AndroidRuntime(5931): at com.example.rpgengine.MainActivity$1.onDoubleTap(MainActivity.java:62)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:498)
03-28 03:19:54.165: E/AndroidRuntime(5931): at com.example.rpgengine.MainActivity.onTouchEvent(MainActivity.java:54)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.app.Activity.dispatchTouchEvent(Activity.java:2399)
03-28 03:19:54.165: E/AndroidRuntime(5931): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.view.View.dispatchPointerEvent(View.java:7307)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.os.MessageQueue.nativePollOnce(Native Method)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.os.MessageQueue.next(MessageQueue.java:125)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.os.Looper.loop(Looper.java:124)
03-28 03:19:54.165: E/AndroidRuntime(5931): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-28 03:19:54.165: E/AndroidRuntime(5931): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 03:19:54.165: E/AndroidRuntime(5931): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 03:19:54.165: E/AndroidRuntime(5931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-28 03:19:54.165: E/AndroidRuntime(5931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 03:19:54.165: E/AndroidRuntime(5931): at dalvik.system.NativeStart.main(Native Method)
我在另一个程序中有相同的代码(我复制了它并只是更改了它调用的函数名称)并且它可以工作。任何帮助,将不胜感激。
谢谢!