java.lang.NullPointerException; android.view.View.dispatchTouchEvent(View.java:7713)

StackOverflow https://stackoverflow.com/questions/22948777

  •  30-06-2023
  •  | 
  •  

Вопрос

I am making an application, it seems to work run very well, but the application has some kind of a bug. Every time i am touching the view and moving my finger outside the view and releasing, it crashes.

Here is an explanation of my problem:![enter image description here][1]

Here is some of my code that might be the problem:

public class Game extends View implements OnTurnBasedMatchUpdateReceivedListener  {

public Game(GameActivity activity){
    super(activity);
    this.mGameActivity = activity;
    this.mGameBoard    = new GameBoard(activity);
    this.setFocusable(true);
    this.setFocusableInTouchMode(true);
    this.setClickable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    // TODO Subtract movementpoints
    int eventCode = event.getAction();
    switch(eventCode){
    case MotionEvent.ACTION_DOWN:
        return true;
    case MotionEvent.ACTION_MOVE:
        return true;
    case MotionEvent.ACTION_UP:
        return true;
    default:
        return true;            
    }
}

Here is the error log:

04-08 23:00:47.585: E/InputEventReceiver(18313): Exception dispatching input event.
04-08 23:00:47.585: E/MessageQueue-JNI(18313): Exception in MessageQueue callback: handleReceiveCallback
04-08 23:00:47.620: E/MessageQueue-JNI(18313): java.lang.NullPointerException
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at com.boentertainment.quizgame.Game.onTouchEvent(Game.java:199)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.View.dispatchTouchEvent(View.java:7713)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-08 23:00:47.620: E/MessageQueue-JNI(18313):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
Это было полезно?

Решение

You'll need to learn to debug your application. Then you can do the things that cause the bug, and see the values of your variables at that time. Another usefull thing is the logging system. You can find more information here if you're using Eclipse: How to Debug Android application line by line using Eclipse?

Your switch definitely needs break-statements if you are going to implement code for each case in the future:

switch(eventCode){
case MotionEvent.ACTION_DOWN:
    return true;
    break;
case MotionEvent.ACTION_MOVE:
    return true;
    break;
case MotionEvent.ACTION_UP:
    return true;
    break;
default:
    return true;            
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top