当我触摸一个按钮时,我遇到了一个问题,导致我的应用程序崩溃,阅读 LogCat 我发现错误来自 TouchEvent。我没发现问题!这是我的代码:
final Button Dec = (Button)findViewById(R.id.Dec);
Dec.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
SendCmd(Server.getText().toString(), "keydec.down");
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
SendCmd(Server.getText().toString(), "keydec.up");
}
return true;
}
});
以下是 LogCat 中所写的内容:
09-05 16:47:05.626: E/AndroidRuntime(11880): FATAL EXCEPTION: main
09-05 16:47:05.626: E/AndroidRuntime(11880): java.lang.NullPointerException
09-05 16:47:05.626: E/AndroidRuntime(11880): at net.shape.remote.RacingPad$1.onTouch(RacingPad.java:46)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.view.View.dispatchTouchEvent(View.java:3762)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
09-05 16:47:05.626: E/AndroidRuntime(11880): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1731)
09-05 16:47:05.626: E/AndroidRuntime(11880): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1120)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
09-05 16:47:05.626: E/AndroidRuntime(11880): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1715)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.view.ViewRoot.handleMessage(ViewRoot.java:1787)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.os.Looper.loop(Looper.java:123)
09-05 16:47:05.626: E/AndroidRuntime(11880): at android.app.ActivityThread.main(ActivityThread.java:4633)
09-05 16:47:05.626: E/AndroidRuntime(11880): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 16:47:05.626: E/AndroidRuntime(11880): at java.lang.reflect.Method.invoke(Method.java:521)
09-05 16:47:05.626: E/AndroidRuntime(11880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-05 16:47:05.626: E/AndroidRuntime(11880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-05 16:47:05.626: E/AndroidRuntime(11880): at dalvik.system.NativeStart.main(Native Method)