getters从活动类返回null

问题描述 投票:1回答:1

我今天花了很多时间研究为什么我的吸气剂返回null并且似乎无法找到一个干净的答案。我希望使用类创建一个干净的层次结构,并将信息从一个传递到另一个。我不知道这是不是很好的做法,我仍然是游戏开发和Android工作室的新手。我希望能够从Game.java(这是我的活动类)传递变量,而不将其添加到其构造函数中的其他类。我希望能够访问getters,就像它是另一个类,但似乎无法弄清楚如何。完整代码将包含在下面:Game.java中的getXDirection和getyDirecion从播放器类返回0,暗示它们尚未初始化

game.Java

package com.Frenchie.SpaceshipSammy;

import ...

public class Game extends Activity implements SensorEventListener{

    private SensorManager senSensorManager;
    private Sensor senAccelerometer;

    //Directional constants
    private static final int DIRECTION_STATIONARY = 0;
    private static final int DIRECTION_LEFT = 1;
    private static final int DIRECTION_RIGHT= 2;
    private static final int DIRECTION_UP = 3;
    private static final int DIRECTION_DOWN= 4;

    //Direction variables
    private int yDirection, xDirection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Set view to GameView
        setContentView(new GameView(this));

        //Sensor stuff
        senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                //Finger down - Move up
                yDirection = DIRECTION_UP;
                break;
            case MotionEvent.ACTION_UP:
                //Finger lifted - Move down
                yDirection = DIRECTION_DOWN;
                break;
        }
        return true;
    }

    //Overriding Accelerometer to read data
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor mySensor = sensorEvent.sensor;

        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = sensorEvent.values[1];

            if (x > -1 && x < 1) {
                //Stationary
                xDirection = DIRECTION_STATIONARY;
            } else if (x >= 1) {
                //Move right
                xDirection = DIRECTION_RIGHT;
            } else if (x <= -1) {
                //Move left
                xDirection = DIRECTION_LEFT;
            } else {
                Log.d("onSensorChanged", "Escaped");
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    //Getters for Player class to use
    public int getyDirection() {
        return yDirection;
    }

    public int getxDirection() {
        return xDirection;
    }
}

player.Java

package com.Frenchie.SpaceshipSammy;

import ...

public class Player {

    //Directional constants
    private static final int DIRECTION_STATIONARY = 0;
    private static final int DIRECTION_LEFT = 1;
    private static final int DIRECTION_RIGHT = 2;
    private static final int DIRECTION_UP = 3;
    private static final int DIRECTION_DOWN = 4;

    //Location variables
    private int x, y, speed;

    //Sprite
    private Bitmap sprite;

    private Game userInput;

    public Player(Context context){

        speed = 10;

        userInput = new Game();

        sprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.player);
    }

    //Called from Logic to move players location
    public void PositionUpdate(){
        xMove();
        yMove();
    }

    private void xMove(){
        if (userInput.getxDirection() == DIRECTION_STATIONARY){
            //Stationary
        }
        else if (userInput.getxDirection() == DIRECTION_RIGHT){
            //Move right
            x += speed;
            Log.d("xMove","Right");
        }
        else if (userInput.getxDirection() == DIRECTION_LEFT){
            //Move left
            x -= speed;
        }
        else {
            Log.d("xMove", "xDirection unrecognised");
        }
    }

    private void yMove(){
        if (userInput.getyDirection() == DIRECTION_UP){
            //Move up
            y -= speed;
        }
        else if (userInput.getyDirection() == DIRECTION_DOWN){
            //Move down
            y += speed;
        }
        else{
            //Log.d("yMove", "yDirection unrecognised");
        }
    }

    //Get x and y for Logic
    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Bitmap getSprite() {
        return sprite;
    }
}

logic.Java

package com.Frenchie.SpaceshipSammy;

import ...

public class Logic implements Runnable {

    //Bring in required classes
    private Player player;

    //Player variables
    private int yPlayer, xPlayer;
    private Bitmap playerSprite;

    public Logic(Context context){

        player = new Player(context);

        //Sprite currently wont change so this doesn't need to be updated with the location
        playerSprite = player.getSprite();

        //Creating and running thread
        Thread thread = new Thread(this);
        thread.start();
    }

    //Thread to tell the players position update method to run and update the players values in this class
    @Override
    public void run() {
        while(true) {
            player.PositionUpdate();
            PlayerLocation();
        }
    }

    //Updates the players location which can be passed onto GameView
    public void PlayerLocation(){

        xPlayer = player.getX();
        yPlayer = player.getY();
    }

    //Getters for GameView to use
    public int getyPlayer() {
        return yPlayer;
    }

    public int getxPlayer() {
        return xPlayer;
    }

    public Bitmap getPlayerSprite() {
        return playerSprite;
    }
}

game view.Java

package com.Frenchie.SpaceshipSammy;

import ...

public class GameView extends SurfaceView implements Runnable{

    private SurfaceHolder surfaceHolder = getHolder();
    private Canvas canvas;

    //Link Logic class
    private Logic logic;

    public GameView(Context context) {
        super(context);

        //Creates logic as a new object
        logic = new Logic(context);

        //Creates and starts the thread
        Thread thread = new Thread(this);
        thread.start();

    }

    //Override thread method. This is called when the thread is started
    @Override
    public void run() {
        while (true){
            DrawFrame();
        }
    }


    private void DrawFrame(){
        canvas = surfaceHolder.lockCanvas();
        if (surfaceHolder.getSurface().isValid()){
            canvas.drawColor(Color.MAGENTA);
            canvas.drawBitmap(logic.getPlayerSprite(), logic.getxPlayer(), logic.getyPlayer(), null);
            surfaceHolder.unlockCanvasAndPost(canvas);
        } else {
            Log.d("DrawFrame", "Surface Invalid");
        }
    }
}

所有帮助表示赞赏!

java android android-activity null getter
1个回答
0
投票

这是你的问题,在你的GameView

userInput = new Game();

您指向Activity的新实例,而不是显示的实际Activity。在使用活动实例“this”创建Player之后,您需要设置此值。

© www.soinside.com 2019 - 2024. All rights reserved.