2013-01-08 101 views
1

我做了一個例子,擴展SurfaceViewThread在Android上畫布上的一些對象,但我面臨兩個問題。SurfaceView在Surface上崩潰Change Android

  1. 當按下按鈕應用程序關閉,然後告訴我「不幸的是,您的示例已停止」。

  2. 將方向從「縱向」更改爲「橫向」或VS時,應用程序無法調整新屏幕並且不更新繪圖,則它會崩潰並顯示相同的消息「不幸的是,您的示例已停止」。

請查看代碼:

@Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     // TODO implement this method 
     holder.getSurface().setSize(width, height); 
     holder.setFormat(format); 
    }  

@Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // at this point the surface is created and 
     // we can safely start the game loop 
     thread.setRunning(true); 
     thread.start(); 
    } 

@Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 

     // tell the thread to shut down and wait for it to finish 
     // this is a clean shutdown 
     boolean retry = true; 
     while (retry) { 
      try { 
       thread.join(); 
       retry = false; 
      } catch (InterruptedException e) { 
       // try again shutting down the thread 
      } 
     } 
    } 

當我檢查,我發現以下行logcat的是,在這部分

@Override 
    public void run() { 
     Canvas canvas; 
     // initialise timing elements for stat gathering 
     initTimingElements(); 

     long beginTime; // the time when the cycle begun 
     long timeDiff; // the time it took for the cycle to execute 
     int sleepTime; // ms to sleep (<0 if we're behind) 
     int framesSkipped; // number of frames being skipped 

     sleepTime = 0; 

     while (running) { 
      canvas = null; 
      // try locking the canvas for exclusive pixel editing 
      // in the surface 
      try { 
       canvas = this.surfaceHolder.lockCanvas(); 
       LoggerHandler.log("canvas : " + canvas); 
       synchronized (surfaceHolder) { 
        beginTime = System.currentTimeMillis(); 
        framesSkipped = 0; // resetting the frames skipped 
        // update game state 
        this.gamePanel.update(); 
        // render state to the screen 
        // draws the canvas on the panel 
        this.gamePanel.render(canvas); 
        // calculate how long did the cycle take 
        timeDiff = System.currentTimeMillis() - beginTime; 
        // calculate sleep time 
        sleepTime = (int) (FRAME_PERIOD - timeDiff); 

        if (sleepTime > 0) { 
         // if sleepTime > 0 we're OK 
         try { 
          // send the thread to sleep for a short period 
          // very useful for battery saving 
          Thread.sleep(sleepTime); 
         } catch (InterruptedException e) { 
         } 
        } 

        while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) { 
         // we need to catch up 
         this.gamePanel.update(); // update without rendering 
         sleepTime += FRAME_PERIOD; // add frame period to check 
                // if in next frame 
         framesSkipped++; 
        } 

        // for statistics 
        framesSkippedPerStatCycle += framesSkipped; 
        // calling the routine to store the gathered statistics 
        storeStats(); 
       } 
      } finally { 
       // in case of an exception the surface is not left in 
       // an inconsistent state 
       if (canvas != null) { 
        surfaceHolder.unlockCanvasAndPost(canvas); 
       } 
      } // end finally 
     } 
    } 

在此行中的問題的問題:

canvas = this.surfaceHolder.lockCanvas(); 

這是退貨null更改方向時。

+0

你能否提供logcat! –

+0

我發現這個問題,因爲這行代碼 canvas = this.surfaceHolder.lockCanvas();當我改變方向時返回null,你會在帖子中找到這個部分代碼 –

回答

1

我有同樣的問題。解決方案:只檢查畫布是否爲空。 在我的測試中:

  try { 
       canvas = surfaceHolder.lockCanvas(null); 
       if(canvas != null){ 
        synchronized (surfaceHolder) { 
         canvas.drawColor(Color.BLACK); 
         canvas.drawBitmap(picture, 0, 0 , null); 
        } 
       } 
      } 
      finally { 
       if (canvas != null) { 
        surfaceHolder.unlockCanvasAndPost(canvas); 
       } 
      }