Android“不幸已停止”错误

时间:2013-07-04 06:57:15

标签: android

我是Android编程新手。我只是想在android中制作自定义相机。为此,我使用了这个网站(http://developer.android.com/guide/topics/media/camera.html) 虽然我按照网站上的步骤,我得到一个错误“不幸的是你的应用已停止”。我搜索了所有关于“遗憾已经停止”的问题,我在这里阅读了答案。但他们没有帮助我。请帮帮我!

这是我的cameraActivity代码:

public class CameraActivity extends Activity {

protected static final String TAG = null;
protected static final String MEDIA_TYPE_IMAGE = ".jpeg";
private static Camera mCamera;
private CameraPreview mPreview;

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

    // 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);


    final PictureCallback mPicture = new PictureCallback() {

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

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null){
                Throwable e = null;
                Log.d(TAG, "Error creating media file, check storage permissions: " +
                    e.getMessage());
                return;
            }

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }

        private File getOutputMediaFile(String mediaTypeImage) {
            // TODO Auto-generated method stub
            return null;
        }
    };

Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // get an image from the camera
            mCamera.takePicture(null, null, mPicture);
        }});}

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

当我在logcat中点击错误时,它在此代码中向我展示了

"mcamera.setPreviewDisplay(holder);"

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {


private SurfaceHolder mHolder;
private Camera mcamera;

public CameraPreview(Context context,Camera camera){
    super(context);
    mcamera=camera;
    mHolder=getHolder();
    mHolder.addCallback(this);
    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());
    }
}

在LogCat中

E/AndroidRuntime(1071): FATAL EXCEPTION: main
 E/AndroidRuntime(1071): java.lang.NullPointerException
 E/AndroidRuntime(1071):    at com.example.ntry.CameraPreview.surfaceCreated(CameraPreview.java:32)
 E/AndroidRuntime(1071):    at android.view.SurfaceView.updateWindow(SurfaceView.java:569)
 E/AndroidRuntime(1071):    at android.view.SurfaceView.access$000(SurfaceView.java:86)
 E/AndroidRuntime(1071):    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:174)
 E/AndroidRuntime(1071):    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680)
 E/AndroidRuntime(1071):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1842)
 E/AndroidRuntime(1071):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
 E/AndroidRuntime(1071):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
 E/AndroidRuntime(1071):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
 E/AndroidRuntime(1071):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
 E/AndroidRuntime(1071):    at android.view.Choreographer.doFrame(Choreographer.java:532)
 E/AndroidRuntime(1071):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
 E/AndroidRuntime(1071):    at android.os.Handler.handleCallback(Handler.java:725)
 E/AndroidRuntime(1071):    at android.os.Handler.dispatchMessage(Handler.java:92)
 E/AndroidRuntime(1071):    at android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime(1071):    at android.app.ActivityThread.main(ActivityThread.java:5041)
 E/AndroidRuntime(1071):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1071):    at java.lang.reflect.Method.invoke(Method.java:511)
 E/AndroidRuntime(1071):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 E/AndroidRuntime(1071):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 E/AndroidRuntime(1071):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

如果您的应用程序第一次启动时发生,那么您的相机对象为空或持有者为空

但如果您第二次导航到相机视图时会发生这种情况,那么您的相机会保留前一个视图,而第二次无法访问,那么您必须在暂停视图或破坏视图时释放相机

希望它会有所帮助

答案 1 :(得分:0)

从这里看起来getCameraInstance返回null。 Camera.open()是否会引发异常?