简体   繁体   中英

Pass data from an Activity to a Class

i have problems with my game. I have an Main Activity that's shows a surfaceview where i draw my game. Now i want to pass data from my Main Activity to a Class but everytime i try this i got an Error and in the LogCat i see that the error happens on the line where i pass the data. here my Code 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);
}

Class that received the data:

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();
}

and here my LogCat error:

here the logcat:

thread exiting with uncaught exception (group=0x4001b188) Uncaught handler: thread main exiting due to uncaught exception java.lang.NullPointerException at com.whlabs.nameless.Banka.(Banka.java:14) at com.whlabs.nameless.MainActivity.onTouchEvent(MainActivity.java:31) at android.app.Activity.dispatchTouchEvent(Activity.java:2064) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) at 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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method)

I'm guessing your getting a NullPointerException because you're not initializing gameView. One way to fix this is to initialize gameView in your constructor like so:

public Banka (float x, float y, boolean touch){
    this.currentX = x;
    this.currentY = y;
    this.touchDownTrue = touch;
    gameView = new GameView();
    gameView.isReady();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM