. lang。NullPointerException:尝试调用虚拟方法的android.graphics。RectF(复制)

时间:2021-01-24 20:50:52

This question already has an answer here:

这个问题已经有了答案:

I'm kinda new to this all android game development and need a little help..my game is crashing and i cant find to solve the problem..Thank you in advance.

我对所有的android游戏开发都有点陌生,需要一点帮助。我的游戏崩溃了,我无法找到解决这个问题的方法。提前谢谢你。

public class BrickBreaker extends AppCompatActivity {

    BreakerView brickView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        brickView = new BreakerView(this);
        setContentView(brickView);
    }


    private class BreakerView extends SurfaceView implements Runnable {

        Canvas canvas;
        Paint paint;

        Thread gameThread = null;

        SurfaceHolder ourHolder;

        volatile boolean playing;

        boolean paused = true;

        long fps;

        private long timeThisFrame;

        int screenX;
        int screenY;

        Paddle paddle;

        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        public BreakerView(Context context) {
            super(context);
            this.ourHolder = getHolder();
            this.paint = new Paint();

            //res
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);

            screenX = size.x;
            screenY = size.y;
        }


        @Override
        public void run() {
            while (playing) {
                long startFrameTime = System.currentTimeMillis();

                if (!paused) {
                    update();
                }

                draw();

                timeThisFrame = System.currentTimeMillis() - startFrameTime;
                if (timeThisFrame >= 1) {
                    fps = 1000 / timeThisFrame;
                }
            }
        }

        public void update() {
            paddle.update(fps);
        }

        public void draw() {
            if (ourHolder.getSurface().isValid()) {
                canvas = ourHolder.lockCanvas();

                canvas.drawColor(Color.argb(255, 26, 128, 182));

                paint.setColor(Color.argb(255, 255, 255, 255));

                canvas.drawRect(paddle.getRect(), paint);


                ourHolder.unlockCanvasAndPost(canvas);
            }
        }

        public void pause() {
            playing = false;
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                Log.e("Error:", "joining thread");
            }

        }

        public void resume() {
            playing = true;
            gameThread = new Thread(this);
            gameThread.start();
        }

        @Override
        public boolean onTouchEvent(MotionEvent motionEvent) {

            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:

                    paused = false;

                    if(motionEvent.getX() > screenX / 2){
                        paddle.setMovementState(paddle.RIGHT);
                    }
                    else{
                        paddle.setMovementState(paddle.LEFT);
                    }

                    break;

                case MotionEvent.ACTION_UP:

                    paddle.setMovementState(paddle.STOPPED);

                    break;
            }
            return true;
        }


    //end of breakView
    }

    @Override
    protected void onResume() {
        super.onResume();

        brickView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();

        brickView.pause();
    }

and here is the log

这是log。

05-06 15:19:43.119 27243-27287/? E/AndroidRuntime: FATAL EXCEPTION: Thread-30652
                                               Process: com.example.david.brickbreakergame, PID: 27243
                                               java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.RectF com.example.david.brickbreakergame.Paddle.getRect()' on a null object reference
                                                   at com.example.david.brickbreakergame.BrickBreaker$BreakerView.draw(BrickBreaker.java:99)
                                                   at com.example.david.brickbreakergame.BrickBreaker$BreakerView.run(BrickBreaker.java:78)
                                                   at java.lang.Thread.run(Thread.java:818)

1 个解决方案

#1


0  

You have not initialized paddle. So either initialize it, or create a new Rect() with the required coordinates and pass this as the parameter in your drawRect() method

你没有初始化桨。因此,要么初始化它,要么用所需的坐标创建一个新的Rect(),并将它作为drawRect()方法中的参数传递。

#1


0  

You have not initialized paddle. So either initialize it, or create a new Rect() with the required coordinates and pass this as the parameter in your drawRect() method

你没有初始化桨。因此,要么初始化它,要么用所需的坐标创建一个新的Rect(),并将它作为drawRect()方法中的参数传递。