【问题标题】:Getters returning null from activity class从活动类返回 null 的 Getter
【发布时间】:2018-06-02 04:36:55
【问题描述】:

我今天花了很多时间研究为什么我的吸气剂返回 null 并且似乎找不到一个干净的答案。我希望使用类创建一个干净的层次结构并将信息从一个传递到另一个。我不知道这是否是一种好的做法,我对游戏开发和一般的 android 工作室还是新手。我希望能够从 Game.java(这是我的活动类)中传递变量,而无需将其添加到构造函数中的其他类中。我希望能够像访问另一个类一样访问 getter,但似乎无法弄清楚如何。完整代码将包含在下面: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;
    }
}

GameView.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");
        }
    }
}

感谢所有帮助!

【问题讨论】:

  • 哪些 getter 返回空值?
  • Game.java 中的 getyDirection 和 getxDirection。感谢您指出这不是很清楚@elmorabea

标签: java android android-activity null getter


【解决方案1】:

这是你的问题,在你的GameView

userInput = new Game();

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

【讨论】:

  • 谢谢我明白你的意思但是如果我将'Game userInput'声明为变量然后设置'userInput = new Game(this);'我在“this”下得到红线,上面写着 Game() in Game cannot be applied 。你知道为什么或者我完全误解了你吗?
猜你喜欢
  • 2021-05-04
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
相关资源
最近更新 更多