NullPointerException report from android market

这一生的挚爱 提交于 2019-12-24 10:40:06

问题


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.


回答1:


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.




回答2:


The Solution is not to use "setInputType" with a TextView. You don't need input type filtering for TextViews anyway since they are for displaying text only. Input type is only needed for EditText (and there it works). I had the same problem with Android versions below 4.2.

The disadvantage is, that applying input type "password" on a textview actually makes sense as it masks the password, which may be intended (it was in my case). But this causes the crash when touching the textview.



来源:https://stackoverflow.com/questions/5978370/nullpointerexception-report-from-android-market

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!