我的游戏有问题。我有一个主要活动,它显示了我绘制游戏的表面视图。现在我想将数据从我的主要活动传递给一个类,但每次我尝试这个我得到一个错误,在LogCat中我看到错误发生在我传递数据的行上。我的代码 MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
public boolean onTouchEvent(MotionEvent event) {
currentX = event.getX();
currentY = event.getY();
if(event.getAction() == MotionEvent.ACTION_DOWN){
touchDisplay = true;
banka = new Banka(currentX, currentY, touchDisplay);
}
if(event.getAction() == MotionEvent.ACTION_UP){
touchDisplay = false;
banka = new Banka(currentX, currentY, touchDisplay);
}
return super.onTouchEvent(event);
}
收到数据的类:
public class Banka {
public GameView gameView;
public float currentX = 0;
public float currentY = 0;
public boolean touchDownTrue = false;
public Banka (float x, float y, boolean touch){
this.currentX = x;
this.currentY = y;
this.touchDownTrue = touch;
gameView.isReady();
}
这里是我的LogCat错误:
这里是logcat:
线程以未捕获的异常退出(group = 0x4001b188)未捕获 handler:由于未捕获的异常,线程主要退出 java.lang.NullPointerException at com.whlabs.nameless.Banka。(Banka.java:14)at com.whlabs.nameless.MainActivity.onTouchEvent(MainActivity.java:31)at at android.app.Activity.dispatchTouchEvent(Activity.java:2064)at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 在android.view.ViewRoot.handleMessage(ViewRoot.java:1691)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:123)at android.app.ActivityThread.main(ActivityThread.java:4363)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:860) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)at dalvik.system.NativeStart.main(原生方法)
答案 0 :(得分:0)
我猜你得到了NullPointerException,因为你没有初始化gameView。解决此问题的一种方法是在构造函数中初始化gameView,如下所示:
public Banka (float x, float y, boolean touch){
this.currentX = x;
this.currentY = y;
this.touchDownTrue = touch;
gameView = new GameView();
gameView.isReady();
}