2015-07-10 75 views
0

我無法運行我的程序。該應用程序崩潰。我試圖通過touchEvent讓圖像在屏幕上移動。Android nullPointerException錯誤

java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.swapnil.new1.model.conmponents.Speed.getxDirection()' on a null object reference 
    at com.example.swapnil.new1.GamePanel.update(GamePanel.java:89) 
    at com.example.swapnil.new1.MainThread.run(MainThread.java:32) 

我已經添加了Speed類,我認爲這是一個例外。建議更新。

Speed類具有getxDirection方法這是造成異常。

package com.example.swapnil.new1.model.conmponents; 

public class Speed { 
    public static final int DIRECTION_RIGHT = 1; 
    public static final int DIRECTION_LEFT = -1; 
    public static final int DIRECTION_UP = -1; 
    public static final int DIRECTION_DOWN = 1; 

    private float xv = 1; // speed in x 
    private float yv = 1; // speed in y 

    private int xDirection = DIRECTION_RIGHT; 
    private int yDirection = DIRECTION_DOWN; 

    public Speed() { 
     this.xv = 1; 
     this.yv = 1; 
    } 

    public Speed(float xv, float yv) { 
     this.xv = xv; 
     this.yv = yv; 
    } 

    public float getXv() { 
     return xv; 
    } 

    public void setXv(float xv) { 
     this.xv = xv; 
    } 

    public float getYv() { 
     return yv; 
    } 

    public void setYv(float yv) { 
     this.yv = yv; 
    } 

    public int getxDirection() { 
     return xDirection; 
    } 

    public void setxDirection(int xDirection) { 
     this.xDirection = xDirection; 
    } 

    public int getyDirection() { 
     return yDirection; 
    } 

    public void setyDirection(int yDirection) { 
     this.yDirection = yDirection; 
    } 

    public void toggleXDirection() { 
     xDirection = xDirection * -1; 
    } 

    public void toggleYDirection() { 
     yDirection = yDirection * -1; 
    } 
} 

MainThread類:

package com.example.swapnil.new1; 

import android.graphics.Canvas; 
import android.util.Log; 
import android.view.SurfaceHolder; 

public class MainThread extends Thread { 
    private static final String TAG = MainThread.class.getSimpleName(); 
    private boolean running; 
    private SurfaceHolder surfaceHolder; 
    private GamePanel gamePanel; 

    public MainThread(SurfaceHolder surfaceHolder , GamePanel gamePanel) { 
     super(); 
     this.surfaceHolder = surfaceHolder; 
     this.gamePanel = gamePanel; 
    } 

    public void setRunning(boolean running) { 
     this.running = running; 
    } 

    @Override 
    public void run() { 
     Canvas canvas; 
     Log.d(TAG , "Starting game loop"); 
     while(running) { 
      canvas = null; 
      try { 
       canvas = this.surfaceHolder.lockCanvas(); 
       synchronized (surfaceHolder) { 
        this.gamePanel.render(canvas); 
        this.gamePanel.update(); 
       } 
      } finally { 
       if(canvas != null) { 
        surfaceHolder.unlockCanvasAndPost(canvas); 
       } 
      } 
     } 
    } 
} 

enter image description here

GamePanel類:

import android.app.Activity; 
import android.content.Context; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

import com.example.swapnil.new1.model.Droid; 
import com.example.swapnil.new1.model.conmponents.Speed; 

public class GamePanel extends SurfaceView implements 
     SurfaceHolder.Callback { 

    private static final String TAG = GamePanel.class.getSimpleName(); 

    private MainThread thread; 
    private Droid droid; 

    public GamePanel(Context context) { 
     super(context); 
     getHolder().addCallback(this); 
     droid = new Droid(BitmapFactory.decodeResource(getResources(),R.drawable.droid_1),50,50); 
     thread = new MainThread(getHolder() , this); 
     setFocusable(true); 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     thread.setRunning(true); 
     thread.start(); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     boolean retry = true; 
     while (retry) { 
      try { 
       thread.join(); 
       retry = false; 
      }catch (InterruptedException e) { 
      } 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      droid.handleActionDown((int)event.getX(),(int)event.getY()); 
      if (event.getY() > getHeight() - 50) { 
      thread.setRunning(false); 
       ((Activity)getContext()).finish(); 
      } else { 
       Log.d(TAG , "Coords : x = " + event.getX() + ",y = " + event.getY()); 
      } 
     } 

     if (event.getAction() == MotionEvent.ACTION_MOVE) { 
      if (droid.isTouched()) { 
       droid.setX((int)event.getX()); 
       droid.setY((int) event.getY()); 
      } 
     } 
     if (event.getAction() == MotionEvent.ACTION_UP) { 
      if (droid.isTouched()) { 
       droid.setTouched(false); 
      } 
     } 
     return true; 
    } 

    // changed protected to public 
    protected void render(Canvas canvas) { 
     canvas.drawColor(Color.BLACK); 
     droid.draw(canvas); 
    } 

    public void update() { 
     // collision with right 
     if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT && 
       droid.getX() + droid.getBitmap().getWidth()/2 >= getWidth()) { 
      droid.getSpeed().toggleXDirection(); 
     } 
     //collision with left 
     if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT && 
       droid.getX() - droid.getBitmap().getWidth()/2 <= 0) { 
      droid.getSpeed().toggleXDirection(); 
     } 
     // collision with down 
     if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN && 
       droid.getY() + droid.getBitmap().getHeight()/2 >= getHeight()) { 
      droid.getSpeed().toggleYDirection(); 
     } 
     //collision with up 
     if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP && 
       droid.getY() - droid.getBitmap().getHeight()/2 <= 0) { 
      droid.getSpeed().toggleYDirection(); 
     } 
     droid.update(); 
    } 
} 
+3

如果你指出哪些行是32和89,我們就不會看到行號,它會加快速度。 – dbillz

+0

update()方法中的你的機器人對象爲null。 –

+0

'droid'在GamePanel構造函數中初始化。可能你的初始化語句爲'droid' - 'droid = new Droid(BitmapFactory.decodeResource(getResources(),R.drawable.droid_1),50,50);' - 在某種程度上被打破了? – Nerdizzle

回答

0

速度場在你的Droid對象未設置。在您的GamePanel.update()方法中,您在此字段上調用getxDirection並導致NullPointerException。