2011-11-15 64 views
0

在SurfaceCreated的setPreviewDisplay(holder)中,應用程序崩潰(在任何開始之前有一個對話框必須強制關閉,然後應用程序退出),我找不到原因。 請指教。以下是我的代碼。使用SurfaceView崩潰的相機應用程序

public void onCreate(Bundle savedInstanceState) { 
     cameraPreview = new CameraPreview();//CameraPreview has a methos to open the Camera 
     cameraObject = CameraPreview.getCameraInstance(); 
     mHolder = previewSurface.getHolder();//previewSurface is the SurfaceView declared in XML and then I'm doinf findViewById 
     mHolder.addCallback(this); 

} 

    public void surfaceCreated(SurfaceHolder holder) {   

    // The Surface has been created, now tell the camera where to draw the preview.  
    try {   
      cameraObject.setPreviewDisplay(holder); //CRASHES HERE   
      //cameraObject.startPreview();  
     } catch (IOException e) { 
      Log.d(TAG, "Error setting camera preview: " + e.getMessage());  
     } 
} 

public void surfaceDestroyed(SurfaceHolder holder) {  
    // empty. Take care of releasing the Camera preview in your activity.  
} 

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
     // If your preview can change or rotate, take care of those events here.  
     // Make sure to stop the preview before resizing or reformatting it.   
     if (mHolder.getSurface() == null){   
      // preview surface does not exist   
       return;   
      }   
     // stop preview before making changes  
     try {    
      cameraObject.stopPreview();  
      } catch (Exception e){   
        // ignore: tried to stop a non-existent preview  
      }  
      // make any resize, rotate or reformatting changes here  
      // start preview with new settings  
      try {   
       cameraObject.setPreviewDisplay(mHolder);  
       cameraObject.startPreview();   
       } catch (Exception e){  
         Log.d(TAG, "Error starting camera preview: " + e.getMessage());  
       } 
} 

+1

提供日誌錯誤。你有什麼特別的例外? – user370305

+0

請提供來自logcat的應用程序崩潰的錯誤堆棧跟蹤 – Maneesh

回答

0

在surfaceCreated,只是setPreviewDisplay之前,它需要Camera.open,然後調用相機對象上所述方法。 這兩者之間不能有任何其他陳述,否則它會崩潰。