自定义相机崩溃应用程序onLanuch

时间:2014-07-27 16:55:19

标签: java android camera surfaceview android-logcat

我是一名Android新手,正在尝试实施自定义相机应用。

我跟着这里的turotial: http://developer.android.com/guide/topics/media/camera.html

但每次我尝试启动应用程序时都会崩溃。

我的代码发布在下面:

MainActivity

      import android.app.Activity;
      import android.hardware.Camera;
     import android.os.Bundle;
     import android.view.Menu;
     import android.widget.FrameLayout;

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

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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);
}

public static 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
}

@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;
}

}

CameraPreview类

    package com.studio9ja.smileycamera;
   import java.io.IOException;

   import android.content.Context;
   import android.hardware.Camera;
    import android.util.Log;
   import android.view.SurfaceHolder;
   import android.view.SurfaceView;
   import android.widget.Toast;



  public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private String 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());
    }
}
}        

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
  >
  <FrameLayout
  android:id="@+id/camera_preview"
 android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  />

  <Button
  android:id="@+id/button_capture"
  android:text="Capture"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  />
  </LinearLayout>

我的Logcat

     07-27 17:49:30.928: I/SurfaceView(2348): visibleChanged -- surfaceCreated
    07-27 17:49:30.928: I/SurfaceView(2348): finishedDrawing
    07-27 17:49:30.931: D/AndroidRuntime(2348): Shutting down VM
    07-27 17:49:30.931: W/dalvikvm(2348): threadid=1: thread exiting with uncaught  exception (group=0x410839a8)
      07-27 17:49:30.932: E/AndroidRuntime(2348): FATAL EXCEPTION: main
      07-27 17:49:30.932: E/AndroidRuntime(2348): java.lang.NullPointerException
      07-27 17:49:30.932: E/AndroidRuntime(2348):   at       com.studio9ja.smileycamera.CameraPreview.surfaceCreated(CameraPreview.java:34)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at  android.view.SurfaceView.updateWindow(SurfaceView.java:606)
     07-27 17:49:30.932: E/AndroidRuntime(2348):    at android.view.SurfaceView.access$000(SurfaceView.java:88)
     07-27 17:49:30.932: E/AndroidRuntime(2348):    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:183)
     07-27 17:49:30.932: E/AndroidRuntime(2348):    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:692)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.view.Choreographer.doCallbacks(Choreographer.java:579)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.view.Choreographer.doFrame(Choreographer.java:548)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.os.Handler.handleCallback(Handler.java:725)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at android.os.Handler.dispatchMessage(Handler.java:92)
  07-27 17:49:30.932: E/AndroidRuntime(2348):   at android.os.Looper.loop(Looper.java:153)
 07-27 17:49:30.932: E/AndroidRuntime(2348):    at android.app.ActivityThread.main(ActivityThread.java:5297)
  07-27 17:49:30.932: E/AndroidRuntime(2348):   at java.lang.reflect.Method.invokeNative(Native Method)
    07-27 17:49:30.932: E/AndroidRuntime(2348):     at java.lang.reflect.Method.invoke(Method.java:511)
     07-27 17:49:30.932: E/AndroidRuntime(2348):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
     07-27 17:49:30.932: E/AndroidRuntime(2348):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
     07-27 17:49:30.932: E/AndroidRuntime(2348):    at   dalvik.system.NativeStart.main(Native Method)
      07-27 17:49:30.982: D/dalvikvm(2348): threadid=11: interp stack at 0x5e19a000
      07-27 17:49:32.808: I/Process(2348): Sending signal. PID: 2348 SIG: 9

我还在manifest.xml中添加了以下权限

      <uses-permission android:name="android.permission.CAMERA" />
      <uses-feature android:name="android.hardware.camera" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

你的相机可能是空的。你应该打印例外,如果有一个只是要看,并记录以确保相机仍然不是空的:

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        e.printstacktrace();

    }
    return c; // returns null if camera is unavailable
}

然后在调用预览之前检查以确保它不为空:

if(mCamera!=null){
mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
}