Android:未捕获异常导致主要致命异常

时间:2014-01-19 22:44:19

标签: java android exception android-camera

您好我正在编写一个直接与机器人相机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 dalvik.system.DexClassLoader@40528bb8
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)

我完全遵循了它,但是当我尝试运行它时,我仍然在log-cat中获得并且未捕获异常,这是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) {
   }

}

2 个答案:

答案 0 :(得分:0)

尝试在surfaceCreated方法中捕获顶级异常(而不是IOException),并在异常块中调用e.getMessage()以获取有关错误类型的更多信息。

答案 1 :(得分:0)

Camera.startPreview()投掷RuntimeException可能有不同的原因。对于example,可以通过设置预览尺寸而不是支持的预览尺寸之一。