2

我正在尝试将 Android 的相机与 SurfaceView 一起使用,以便用户可以预览相机。我遵循了 StackOverflow 上的各种教程,最后得到了谷歌自己的教程,但我的应用程序崩溃了。这是我的代码。我正在使用 Eclipse 的 Android 虚拟设备、模拟相机(我认为这不会影响任何东西)和分配的 100mb sd 卡空间。

有两个类,CameraPreview 对象和CameraActivity。

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{

    private SurfaceHolder mHolder;
    private Camera mCamera;
    private final String TAG = "TAG";

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.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 {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}


public class CameraActivity extends Activity{
 private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }
}

06-04 14:28:07.726: W/dalvikvm(6115): threadid=1: 线程退出未捕获异常 (group=0x40a71930) 06-04 14:28:07.956: E/AndroidRuntime(6115): 致命异常: main 06-04 14:28:07.956: E/AndroidRuntime(6115): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.mobilequote/com.example.mobilequote.CameraActivity}: java.lang.NullPointerException 06- 04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app。 ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 06-04 14:28:07.956 : E/AndroidRuntime(6115): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.os.Handler.dispatchMessage(Handler.java:99) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在android.os.Looper.loop(Looper.java:137) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.ActivityThread.main(ActivityThread.java:5041) 06-04 14: 28:07.956: E/AndroidRuntime(6115): 在 java.lang.reflect.Method.invokeNative(Native Method) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 java.lang.reflect.Method。调用(Method.java:511)06-04 14:28:07.956:E/AndroidRuntime(6115):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)06-04 14: 28:07.956: E/AndroidRuntime(6115): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 dalvik.system .NativeStart。main(本机方法)06-04 14:28:07.956:E/AndroidRuntime(6115):由:java.lang.NullPointerException 06-04 14:28:07.956:E/AndroidRuntime(6115):在 com.example。 mobilequote.CameraActivity.onCreate(CameraActivity.java:24) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.Activity.performCreate(Activity.java:5104) 06-04 14:28: 07.956: E/AndroidRuntime(6115): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread .java:2144) 06-04 14:28:07.956: E/AndroidRuntime(6115): ... 11 更多28:07.956: E/AndroidRuntime(6115): 在 android.app.Activity.performCreate(Activity.java:5104) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1080) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-04 14:28:07.956: E/AndroidRuntime (6115): ... 11 更多28:07.956: E/AndroidRuntime(6115): 在 android.app.Activity.performCreate(Activity.java:5104) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1080) 06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-04 14:28:07.956: E/AndroidRuntime (6115): ... 11 更多

CameraActivity.java:24 行是 preview.addView(mPreview);

4

1 回答 1

1

请参阅我对模拟器的评论。但是您的应用程序崩溃的原因是因为您捕获了相机无法打开并返回 null 的异常。然后在您的 CameraPreview 类中,将此空值保存到“mCamera”,然后在 surfaceCreated 中取消引用它:

mCamera.setPreviewDisplay(holder);

所以你最初的问题是它为什么会崩溃……它崩溃是因为你没有很好地处理错误情况。

于 2013-06-04T15:12:04.153 回答