java.lang.NullPointerException:尝试调用虚方法' android.graphics.RectF

时间:2016-05-06 12:29:30

标签: android nullpointerexception

我对这个所有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();
    }

这是日志

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 个答案:

答案 0 :(得分:0)

您尚未初始化划水板。所以要么初始化它,要么创建一个带有所需坐标的新Rect()并将其作为drawRect()方法中的参数传递