2011-11-30 78 views
1

我在Android game.I工作是面臨着同樣的問題,因爲here的onResume後,Android空指針異常的遊戲循環

我試圖等待,通知所有的解決方案,儘快出過there.But應用程序繼續我得到空指針異常。 這裏是日誌信息。

INFO/System.out(8166): on resume 
INFO/System.out(8166): !!!! IN THE GAME RESUME ---- >>> 
INFO/System.out(8166): SURFACE CREATED 
INFO/System.out(8166): thread on resume 
INFO/System.out(8166): in thread game pause=true 
WARN/dalvikvm(8166): threadid=7: thread exiting with uncaught exception (group=0x4001d800) 
ERROR/AndroidRuntime(8166): FATAL EXCEPTION: Thread-11 
ERROR/AndroidRuntime(8166): java.lang.NullPointerException 
ERROR/AndroidRuntime(8166):  at com.org.GummyBlast.GameView.onDraw(GameView.java:442) 
ERROR/AndroidRuntime(8166):  at com.org.GummyBlast.GameLoopThread.run(GameLoopThread.java:88) 

內onSurfaceCreated的代碼,並在我的GameThread類

public class GameLoopThread extends Thread { 
static final long FPS = 10; 
private GameView view; 
public static boolean running = false; 
public static boolean run = true; 
public static boolean game_pause=true; 
private static Object mPauseLock; 
private boolean mFinished=false; 
long ticksPS = 1000/FPS; 
    long startTime; 
    long sleepTime; 
SurfaceHolder holder; 
public GameLoopThread(GameView view) { 
    mPauseLock = new Object(); 

    this.view = view; 
} 

public GameLoopThread() { 
    mPauseLock = new Object(); 
} 


public void setRunning(boolean run) { 
    running = run; 
} 




@Override 
public void run() { 
    while (running) { 
     // Do stuff. 
     System.out.println("in thread game pause="+Boolean.toString(game_pause)); 
     if (game_pause == true) { 
      Canvas c = null; 
      startTime = System.currentTimeMillis(); 
      try { 
       c = view.getHolder().lockCanvas(); 
       synchronized (view.getHolder()) { 
        view.onDraw(c); 

       } 

      } finally { 
       if (c != null) { 
        view.getHolder().unlockCanvasAndPost(c); 
       } 
      } 

      sleepTime = ticksPS - (System.currentTimeMillis() - startTime); 
      try { 
       if (sleepTime > 0) 
        sleep(sleepTime); 
       else 
        sleep(10); 
      } catch (Exception e) { 
       System.out.println("Error in game loop thread "+ e.getMessage()); 

      } 
     } 
     else{ 
     synchronized (mPauseLock) { 
      while (game_pause==false) { 
       try { 
        mPauseLock.wait(); 
       } catch (InterruptedException e) { 
       } 
      } 
     } 
     } 
    } 
} 
public void onPause() { 
    synchronized (mPauseLock) { 
     game_pause = false; 
     System.out.println("thread on pause"); 
    } 
} 

/** 
* Call this on resume. 
*/ 
public void onResume() { 
    synchronized (mPauseLock) { 
     game_pause = true; 
     mPauseLock.notifyAll(); 
     System.out.println("thread on resume"); 
    } 
} 

}

Activity類的onPause

onSurfaceDestroyed

private void init() { 

    gameLoopThread = new GameLoopThread(this); 
    holder = getHolder(); 
    holder.addCallback(new SurfaceHolder.Callback() { 

     @Override 
     public void surfaceDestroyed(SurfaceHolder holder) { 
      System.out.println(" SURFACE DESTROYED"); 

      nullImages(); 
      boolean retry = true; 

      //gameLoopThread.setRunning(false); 
      if(!mGameIsRunning){ 
      while (retry) { 
       try { 
        gameLoopThread.join(); 

        retry = false; 
       } catch (InterruptedException e) { 
       } 
      } 
     } 


    } 

     @Override 
     public void surfaceCreated(SurfaceHolder holder) { 
      System.out.println(" SURFACE CREATED"); 
      Log.d("surface create", "SURFACE CREATED"); 

      /*gameLoopThread.setRunning(true); 

      gameLoopThread.start();*/ 
      start(holder); 

     } 

     public void start(SurfaceHolder holder) { 
      if (!mGameIsRunning) { 

       gameLoopThread.setRunning(true); 
       gameLoopThread.start(); 
       mGameIsRunning = true; 
      } else { 

       gameLoopThread.onResume(); 
      } 
     } 

代碼

如何在onResume中保持遊戲狀態? 感謝

+1

哪條線是442? – Pentium10

+0

第442行是我的onDraw方法中的行,第88行是view.onDraw(c);在我的Thread類中的while循環內部 – jimmyreddevil

+0

但是那裏沒有442行,哪一行是確切的行? – Pentium10

回答

0

也許你需要調用onResume()您超:

public void onResume() { 
    super.onResume(); 
} 

希望這有助於你。

+0

我在我的Class中調用了onResume,但是我正在恢復GameView類中的onSurfaceCreated方法中的線程。 – jimmyreddevil