2013-05-28 61 views
2

我是Android新手。我正在嘗試使用表面視圖開發遊戲應用程序。該應用程序運行正常,但最後我退出時顯示空指針例外onDraw()方法和應用程序崩潰。請幫助我,並提前致謝。 這裏是我的代碼: -Surfaceview android

public class Gameloopthread extends Thread { 

    private Gameview view; 
    static final long fps=30; 
    boolean running; 

    public Gameloopthread(Gameview view) 
    { 
     this.view=view; 
    } 
    // 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 
    public void setRunning(Boolean run) 
    { 
     running=true; 
    } 
    @Override 
    public void run() { 
     long tickps=3000/fps; 
     long startTime=0; 
     long sleepTime; 

     while(running) 
     { 
      Canvas c=null; 


      try{ 
       c=view.getHolder().lockCanvas(); 
       //Log.d("Canvas==>",""+c); 
       synchronized(view.getHolder()){ 
        view.onDraw(c); 

       } 
      }finally{ 
       if(c!=null){ 
        view.getHolder().unlockCanvasAndPost(c); 
       } 
      } 
      sleepTime=tickps-(System.currentTimeMillis()-startTime); 
      try{ 
       if(sleepTime>0) 
       { 
        sleep(sleepTime); 
       }else{ 
        sleep(30); 
       } 
      }catch(Exception e){ 

      } 
     } 
     super.run(); 

    } 
// 

} 
+2

onDraw()代碼和logcat輸出將會很有用。 – sandrstar

+0

是的,請發佈logcat! – thiagolr

+0

搜索此錯誤的計算器。有幾個問題(包括我的一個問題)可以解決這個問題。 – gsingh2011

回答