在我的一个小项目中,我尝试将Point
中的Point
设置为另一个OnTouchEvent()
。这两个点都在构造函数中创建,如下所示:
package com.example.samuel.truespeedgame;
import android.content.Context;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private SurfaceViewThread surfaceViewThread;
public Point clickPoint, playerPoint, startPoint;
GamePanel(Context context) {
super(context);
maxSpeed = 20;
playerPoint = new Point(200,200); //very normal instanciating of Points i guess
startPoint = new Point(200,200);
clickPoint = new Point(200,200);
}
我的OnTouchEvent()
如下:
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
// Player has touched the screen
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if(clickPoint != null)
clickPoint.set((int)motionEvent.getX(),(int)motionEvent.getY());
else if(clickPoint == null) clickPoint = new Point((int)motionEvent.getX(),(int)motionEvent.getY());
startPoint = playerPoint; //Here gets the error thrown
break;
case MotionEvent.ACTION_UP:
}
return true;
}
在重建项目之前,这是 NullPointerException 。现在playerPoint.x
和.y
始终为 0 。
答案 0 :(得分:0)
如果不实例化GamePanel,则playerPoint将为null。