2015-07-04 90 views
0

即時通訊嘗試開發遊戲app.I實現onResume和onPause方法。 onPause方法工作正常,但onResume方法壓碎。canvas返回null onResume方法

@Override 
       public void surfaceCreated(SurfaceHolder holder) { 
        // TODO Auto-generated method stub 
        resume(); 
       } 
public void resume() 
    { 
     addAnime(); 
     myThread.setRunning(true); 
     myThread.start(); 
    } 

如果我不打電話更新onResume方法myApp工作正常,但如果我暫停遊戲,並再次恢復它模擬器說,應用程序沒有響應。

如果我的onResume方法調用resume它說,畫布是空的第一個運行過程中出現

@Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     view.resume(); 
    } 

我不明白爲什麼畫布是空的應用程序的開頭,當我打電話簡歷onResume.Someone給我解釋一下?

編輯 puase():

public void pause() 
    { 
     boolean repaet=true; 
     while(repaet) 
     { 
      try { 
       myThread.join(); 
       myThread.setRunning(false); 
       repaet=false; 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
     myThread=null; 
    } 

EDIT2 MyThread的

public void run() { 
     long ticksPS = 1000/FPS; 
     long startTime; 
     long sleepTime; 
     while (running) { 
      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) {} 
     } 

EDIT3 這裏是我的一套運行過程中出現的功能

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

回答

0

暫停方法:

public void pause() 
    { 
     boolean repeat = myThread != null; 
     while(repeat) 
     { 
      try { 
       myThread.setRunning(false); 
       myThread.join(); 
       repeat=false; 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
     myThread=null; 
    } 

MyThread的:

public class gameThread extends Thread { 
    private SurfaceView view; 
    private boolean isRunning; 

    private static final int FPS; // set FPS with your value 

    private void sleep(long time) throws InterruptedException 
    { 
     Thread.sleep(time); 
    } 

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

    public boolean isRunning() 
    { 
     synchronized(this) 
     { 
      return this.isRunning; 
     } 
    } 

    public void run() 
    { 
     long ticksPS = 1000/FPS; 
    long startTime; 
    long sleepTime; 
    while (isRunning()) { 
     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) {} 
    } 
} 

恢復方法:

public void resume() 
    { 
     addAnime(); 
     // you must create a new thread. (You cannot use this same thread if it end) 
     myThread = new gameThread(surfaceView); 
     myThread.setRunning(true); 
     myThread.start(); 
    } 
+0

即時使用類的視圖多數民衆贊成爲什麼我的箱子公共布爾isOk = false;在主要活動中,我使它main.isOk = true;在resume()之後;並作出if(isOk)view.resume。我仍然看到同樣的錯誤。我錯過了什麼嗎? – zeto

+0

抱歉修復了錯誤,但在暫停和恢復應用程序後仍未恢復。在resumue中setRunning = true並在我的代碼中暫停setRunning = false。 Logcat日誌就像Thread [2,tid = 10408,WaitingInMainSignalCatcherLoop,Thread * = 0xaf570400,peer = 0x12c00080,「Signal Catcher」]:對信號做出反應3 – zeto

+0

@zeto我不知道在哪裏,但是在你的代碼中你有阻塞主線程和應用程序不能繪製任何視圖和系統報告它與應用程序沒有響應(這是一個線索)。嘗試在你的代碼中找到你在哪裏調用無限循環或什麼可以阻止主線程。粘貼onPause方法,也許我找到了一些東西。 – krystian71115

0

嗨,你可以找到的文檔中的詳細信息。 SurfaceHolder.lockCanvas

開始編輯表面中的像素。返回的Canvas可以用來繪製表面的位圖。如果尚未創建表面或其他無法編輯返回空。您通常需要執行Callback.surfaceCreated以找出Surface何時可供使用。 表面的內容是永遠不會保留之間unlockCanvas()和lockCanvas(),因此,必須寫入表面區域內的每個像素。這條規則的唯一例外是指定了一個髒矩形,在這種情況下,將保留非髒像素。 如果在Surface尚未準備好(在Callback.surfaceCreated之前或Callback.surfaceDestroyed之後)時重複調用此函數,您的調用將被抑制到較慢的速率以避免消耗CPU。

關於使用線程或Runneables,你可以檢查這個。 https://guides.codepath.com/android/Repeating-Periodic-Tasks http://developer.android.com/guide/components/processes-and-threads.html