A user submitted the following crash report:
java.lang.NullPointerException
at android.widget.TextView.onTouchEvent(TextView.java:7202)
at android.view.View.dispatchTouchEvent(View.java:3778)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:886)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:886)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:886)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:886)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:886)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1716)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1124)
at android.app.Activity.dispatchTouchEvent(Activity.java:2125)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1700)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1822)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5068)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
I can’t reproduce it on any of my test phones (Xperia, HTC Desire, Galaxy S) (or the emulators), and the error report doesn’t tell me where in the app the problem is or on what device (or who the user was so I can ask them for more information about what they were doing that caused it). I can’t find anywhere in the code where I obviously try to use a TextView via findViewById without checking if it’s null first. None of the xml files have android:onclick in them, so it’s not trying to call a mistyped function or something like that. I tried comparing the lines given to the android source, but they don’t seem to match directly, and reading the source of the named methods hasn’t helped me. (Disclaimer: I’ve only been using Java for a couple of months.)
Anyone have any idea what else I should check?
ETA: This is only place I have a touchlistener instead of a clicklistener is here:
btnDelete.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent meMotion)
{
int iAction = meMotion.getAction();
switch (iAction)
{
case MotionEvent.ACTION_DOWN:
if (false == bKeyPressed)
{
bKeyPressed = true;
deleteANumber();
}
mHandler.removeCallbacks(mDeleteTouch);
mHandler.postAtTime(mDeleteTouch, SystemClock.uptimeMillis()
+ BUTTON_DELAY);
break;
case MotionEvent.ACTION_UP:
bKeyPressed = false;
mHandler.removeCallbacks(mDeleteTouch);
break;
}
return true;
}
});
where mDeleteTouch is
private final Runnable mDeleteTouch = new Runnable()
{
public void run()
{
// delete a number from the display
deleteANumber();
// then call self in BUTTON_DELAY ms, unless cancelled
mHandler.postAtTime(this, SystemClock.uptimeMillis() + BUTTON_DELAY);
}
};
and deleteANumber() is
protected void deleteANumber()
{
String strNumber = tvNumber.getText().toString();
// only remove a character if there is at least one:
if (strNumber.length() > 0)
{
strNumber = strNumber.substring(0, strNumber.length() - 1);
tvNumber.setText(strNumber);
}
}
which seems like it could be the problem as you can’t call null.length(), but tvNumber is created with its text set to “”, so getText().toString() should always return a non null value, right?
ETA2 I’ve managed to get the same error on an HTC Desire HD, but it seems to crash without going back into my code — setting breakpoints immediately inside the onClick doesn’t catch anything.
ETA3 If I comment out tvNumber.setInputType(InputType.TYPE_CLASS_NUMBER); the problem goes away.
On the HTC Desire HD, touching a TextView with setInputType(InputType.TYPE_CLASS_NUMBER) set appears to throw a NullPointerException. Leaving that setting out prevents the crash, but necessitates manual input filtering (which isn’t a problem in my case, but probably is in other people’s). Not a good solution, but a solution nethertheless.