2014-01-19 73 views
0

你好我寫一個簡單的應用程序,與機器人攝像頭API直接交互,我使用本教程http://www.tutorialspoint.com/android/android_camera.htm安卓:未捕獲的異常引起致命的異常在主

誰能告訴我在哪裏,我錯了?

日誌輸出:

01-09 23:42:56.901: D/ATRecorder(886): com.htc.autotest.dlib.RecordEngine in loader [email protected] 
01-09 23:42:57.161: D/AndroidRuntime(886): Shutting down VM 
01-09 23:42:57.161: W/dalvikvm(886): threadid=1: thread exiting with uncaught exception (group=0x400205a0) 
01-09 23:42:57.171: E/AndroidRuntime(886): FATAL EXCEPTION: main 
01-09 23:42:57.171: E/AndroidRuntime(886): java.lang.RuntimeException: startPreview failed 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.hardware.Camera.startPreview(Native Method) 
01-09 23:42:57.171: E/AndroidRuntime(886): at com.example.camera1.ShowCamera.surfaceCreated(ShowCamera.java:30) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.SurfaceView.updateWindow(SurfaceView.java:551) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.View.draw(View.java:6973) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.widget.FrameLayout.draw(FrameLayout.java:357) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1732) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.View.draw(View.java:6973) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.widget.FrameLayout.draw(FrameLayout.java:357) 
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1997) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.draw(ViewRoot.java:1600) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.performTraversals(ViewRoot.java:1321) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.handleMessage(ViewRoot.java:1957) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.os.Looper.loop(Looper.java:150) 
01-09 23:42:57.171: E/AndroidRuntime(886): at android.app.ActivityThread.main(ActivityThread.java:4277) 
01-09 23:42:57.171: E/AndroidRuntime(886): at java.lang.reflect.Method.invokeNative(Native Method) 
01-09 23:42:57.171: E/AndroidRuntime(886): at java.lang.reflect.Method.invoke(Method.java:507) 
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
01-09 23:42:57.171: E/AndroidRuntime(886): at dalvik.system.NativeStart.main(Native Method) 

我完全跟着它,但我仍然得到和未捕獲的異常日誌貓,當我嘗試運行它,這裏是MainActivity代碼:

package com.example.camera1; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

     private Camera cameraObject; 
     private ShowCamera showCamera; 
     private ImageView pic; 
     public static Camera isCameraAvailiable(){ 
      Camera object = null; 
      try { 
      object = Camera.open(); 
      } 
      catch (Exception e){ 
      } 
      return object; 
     } 

     private PictureCallback capturedIt = new PictureCallback() { 

      @Override 
      public void onPictureTaken(byte[] data, Camera camera) { 

      Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length); 
      if(bitmap==null){ 
      Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show(); 
      } 
      else 
      { 
      Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();  
      } 
      cameraObject.release(); 
     } 
    }; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      pic = (ImageView)findViewById(R.id.imageView1); 
      cameraObject = isCameraAvailiable(); 

      showCamera = new ShowCamera(this, cameraObject); 
      FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
      preview.addView(showCamera); 
     } 
     public void snapIt(View view){ 
      cameraObject.takePicture(null, null, capturedIt); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      getMenuInflater().inflate(R.menu.main, menu); 
      return true; 
     } 
    } 

這裏是ShowCamera類:

package com.example.camera1; 

import java.io.IOException; 

import android.content.Context; 
import android.hardware.Camera; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback { 

    private SurfaceHolder holdMe; 
    private Camera theCamera; 

    public ShowCamera(Context context,Camera camera) { 
     super(context); 
     theCamera = camera; 
     holdMe = getHolder(); 
     holdMe.addCallback(this); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     try { 
     theCamera.setPreviewDisplay(holder); 
     theCamera.startPreview(); 
     } catch (IOException e) { 
     } 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder arg0) { 
    } 

} 
+1

什麼是錯誤? –

+0

嗨,對不起,我已經發布在問題的頂部。 –

+0

你能發佈完整的錯誤嗎?你發佈的帖子並不是很豐富。 –

回答

0

Camera.startPreview()RuntimeException可能有不同的原因。對於example,可以通過將預覽大小設置爲不支持預覽大小之一。

0

試圖抓住在surfaceCreated頂層異常方法(而不是IOException)並在異常塊中調用e.getMessage()以獲取有關錯誤類型的更多信息。

+1

這不是一個真正的答案,只是一種方法來獲得答案。對此使用評論。 –

+0

這是一個很好的建議,以捕獲泛型異常,而不僅僅是IOException與相機一起工作。在這種特定情況下,這可以防止應用程序崩潰(儘管作者需要設計回退)。但是沒有附加的信息沒有打印在附件的崩潰日誌中。 –

+0

本應該是一個評論,但我需要50的聲譽才能這樣做。 – Heisenberg

相關問題