I am having a problem causing my application to crash when I touch a button, reading the LogCat i found that the error came from the TouchEvent. I didn’t find the problem! Here’s my code:
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;
}
});
And here’s what’s written in the 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)
Either
Serveris null orServer.getText()is returning null. If you want to be somewhat fault-tolerant you should useString.valueOf(Server.getText()): it will still give you null, but it won’t crash.