0

我正在尝试做一个基本的游戏。我有名为 Ball.java 、 Scratch.java 和 Panel.java 的类

在面板中,我试图从 Ball.java 类中获取信息,但我得到 NullPointerException。我的代码中的错误在哪里?

在 Panel.java 中的此代码中获取异常

public Rect getBoundsBall(){
    return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight() );
}

错误日志

10-13 12:57:28.746: E/AndroidRuntime(367): FATAL EXCEPTION: Thread-10
10-13 12:57:28.746: E/AndroidRuntime(367): java.lang.NullPointerException
10-13 12:57:28.746: E/AndroidRuntime(367):  at com.emredavarci.denemeler.Panel.getBoundsBall(Panel.java:44)
10-13 12:57:28.746: E/AndroidRuntime(367):  at com.emredavarci.denemeler.Panel.update(Panel.java:70)
10-13 12:57:28.746: E/AndroidRuntime(367):  at com.emredavarci.denemeler.TutorialThread.run(TutorialThread.java:30)

面板.java

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

    private TutorialThread _thread;

    Scratch raket=new Scratch();
    Ball top=new Ball();
    Bitmap ball;
    Bitmap _scratch;

    public Panel(Context context) {
        super(context);
        getHolder().addCallback(this);
        _thread = new TutorialThread(getHolder(), this); /
        setFocusable(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) { // to
        switch (event.getAction()){
           case MotionEvent.ACTION_MOVE: // 
           raket.setX((int)(event.getX()));
           break;
       }

       return true;

    }

    public Rect getBoundsBall(){
        return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight() );
    }

    public Rect getBoundsScratch(){
        return new Rect (raket.getX(), raket.getY(),raket.getX()+_scratch.getWidth() ,raket.getY()+_scratch.getHeight() );
    }

    public void update(){ /


        if((top.getX()<480) && (top.getX()>0)){
            top.setX(top.getX()+top.getxdirection());
            }
        if((top.getX()==480) || (top.getX()==0)){
            top.setxdirection(-1);
            top.setX(top.getX()+top.getxdirection());
        }
        if((top.getY()<780) && (top.getY()>0)){
            top.setY(top.getY()+top.getydirection());
            }
        if((top.getY()==780) || (top.getY()==0)){
            top.setydirection(-1);
            top.setY(top.getY()+top.getydirection());
        }


        Rect BallBounds = getBoundsBall();
        Rect ScratchBounds = getBoundsScratch();

        if( BallBounds.intersect(ScratchBounds)     ){              
            top.setydirection(-1); 
            //top.setY(top.getY()); !!!!
        }   

    }





    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }

    public void surfaceCreated(SurfaceHolder holder) {
         _thread.setRunning(true);
         _thread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // simply copied from sample application LunarLander:
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

    @Override
    public void onDraw(Canvas canvas) {  
    _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.red2);
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(_scratch, raket.getX() - (_scratch.getWidth() / 2), raket.getY() - (_scratch.getHeight() / 2), null);
    canvas.drawBitmap(ball, top.getX() - (ball.getWidth() / 2), top.getY() - (ball.getHeight() / 2), null);
    }

}

Ball.java

public class Ball {

    private int x=100;          // the X coordinate
    private int y=100;          // the Y coordinate
    int xdirection=10;
    int ydirection=10;


    public Ball() {
        // TODO Auto-generated constructor stub
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public void setX(int a){
        x=a;
    }

    public void setY(int b){
        y=b;
    }

    public int getxdirection(){
        return xdirection;
    }

    public int getydirection(){
        return ydirection;
    }

    public void setxdirection(int a){
         xdirection=xdirection*a;
    }

    public void setydirection(int b){
        ydirection=ydirection*b;
    }

    //while ile ball u hareket ettir
    //ball un koordinatlarını sakla


}

Scratch.java

public class Scratch {

    private int x = 250; 
    private int y = 600;

    public Scratch() {
        // TODO Auto-generated constructor stub
    }

    public void setX(int a){
        x=a;
    }

    public void setY(int b){
        y=b;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

}
4

2 回答 2

1

您缺少以下内容:

Ball top = new Ball();

即变量top没有被初始化,而是在update方法中使用。

此外,ball初始化. _ onDraw谁知道update之前是否会被调用onDraw

于 2012-10-13T12:34:12.743 回答
1

添加到@Rudolph 的答案中,您确定Bitmap ball已初始化吗?可以肯定的是,尝试记录ball为:

public Rect getBoundsBall(){
    log.v("ball",""+ball);
    return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight() );
}

要么 要么balltop罪魁祸首。找出哪个未初始化并相应地处理它。

于 2012-10-13T13:15:08.563 回答