Android相机示例NullPointerException

时间:2014-06-07 13:55:44

标签: java android nullpointerexception

我正在尝试从Android website实现相机SDK示例代码,但是当我尝试运行应用程序时,我得到了这个Null Pointer Exception并且logcat并没有给我很多信息继续。我是Android开发的新手,所以我不知道如何继续调试这个问题。

06-07 09:48:24.960      927-927/com.luketim.cam.cam2.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.luketim.cam.cam2.app, PID: 927
    java.lang.NullPointerException
            at com.luketim.cam.cam2.app.CameraPreview.surfaceCreated(CameraPreview.java:34)
            at android.view.SurfaceView.updateWindow(SurfaceView.java:572)
            at android.view.SurfaceView.access$000(SurfaceView.java:86)
            at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
            at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1871)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

这是带有抛出此NullPointerException的方法的类。

package com.luketim.cam.cam2.app;

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

import java.io.IOException;

/**
 * Created by luketimothy on 06/06/2014.
 */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "";
    private SurfaceHolder mHolder;
    private Camera mCamera;

    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());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

logcat并没有真正给我提供很多信息

它会告诉您崩溃的代码行(CameraPreview.java:34surfaceCreated()方法)以及崩溃的原因(NullPointerException occurred on that line)

由于surfaceCreated()中只有两行,很明显mCameranull。由于这是从您列出的代码外部提供的,因此您需要追踪打开相机的位置并确定无法按预期工作的原因。

FWIW,在Android中编写相机应用程序相当困难。这不是Android新手应该尝试的东西。我提供a library试图在应用程序中简化相机的使用,但即便如此,它还没有处理设备制造商所做的每件奇怪事情。