我正在制作一个应用程序,它看起来运行得很好,但应用程序有一些错误。每当我触摸视图并将手指移到视图外并释放时,它就会崩溃。
以下是我的问题的解释:![在此输入图像说明] [1]
以下是我的一些代码可能是问题:
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;
}
}
以下是错误日志:
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)
答案 0 :(得分:0)
您需要学习调试应用程序。然后你可以做导致bug的事情,并在那时看到变量的值。另一个有用的东西是日志系统。如果您正在使用Eclipse,可以在此处找到更多信息:How to Debug Android application line by line using Eclipse?
如果您打算在将来为每个案例实现代码,那么您的开关肯定需要break-statements:
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;
}