ボタンにタッチするとアプリケーションがクラッシュするという問題が発生し、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)