我的应用程序应包含全屏CameraPreview。我不知道自己的错误原因,该应用在启动后会迅速关闭。

这是我的代码:

主要活动

    public class MainActivity extends AppCompatActivity{

    private static final String LOG = "MainActivity.java";

    private Camera mCamera;
    private CameraPreview mPreview;

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

    FloatingActionButton fabPref = (FloatingActionButton)        findViewById(R.id.fabPref);
    fabPref.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    FloatingActionButton fabPhoto = (FloatingActionButton) findViewById(R.id.fabPhoto);
    fabPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);


    if(checkCameraHardware(this)==true){
        Log.e("CAMERA", "vorhanden");


        // 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);
        preview.addView(mPreview);

    }else{
        Log.e("CAMERA", "nicht vorhanden");
    }

    }
    private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        return true;
    } else {
        return false;
    }
    }

public Camera getCameraInstance(){
    Camera c = null;
    try {
        releaseCameraAndPreview();
        c = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK); // attempt to get a Camera instance
        Log.e(LOG, "CameraInstance: " + c + " RUNS");
    }
    catch (Exception e){
        Log.e(LOG, "Camera not available");
        Log.e(LOG, "CameraInstance: " + c);
    }
    Log.e(LOG, "CameraInstance: " + c);

    return c; // returns null if camera is unavailable

}

private void releaseCameraAndPreview() {
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
}
    }


这是我的CameraPreview的代码:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private static final String TAG = "CameraPreview.java";

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


这是运行应用程序后的LogCat:

2-16 19:54:06.621 13572-13572/de.aintlarry.shotanalysis I/art: Late-enabling -Xcheck:jni
12-16 19:54:06.622 13572-13572/de.aintlarry.shotanalysis I/art: VMHOOK: rlim_cur : 0 pid:13572
12-16 19:54:06.717 13572-13572/de.aintlarry.shotanalysis W/System: ClassLoader referenced unknown path: /data/app/de.aintlarry.shotanalysis-2/lib/arm64
12-16 19:54:06.834 13572-13572/de.aintlarry.shotanalysis E/CAMERA: vorhanden
12-16 19:54:06.847 13572-13572/de.aintlarry.shotanalysis W/CameraBase: An error occurred while connecting to camera: 0
12-16 19:54:07.149 13572-13572/de.aintlarry.shotanalysis W/CameraBase: An error occurred while connecting to camera: 0
12-16 19:54:07.450 13572-13572/de.aintlarry.shotanalysis W/CameraBase: An error occurred while connecting to camera: 0
12-16 19:54:07.751 13572-13572/de.aintlarry.shotanalysis W/CameraBase: An error occurred while connecting to camera: 0
12-16 19:54:08.052 13572-13572/de.aintlarry.shotanalysis W/CameraBase: An error occurred while connecting to camera: 0
12-16 19:54:08.352 13572-13572/de.aintlarry.shotanalysis I/Camera2Mode: fail
12-16 19:54:08.352 13572-13572/de.aintlarry.shotanalysis E/MainActivity.java: Camera not available
12-16 19:54:08.352 13572-13572/de.aintlarry.shotanalysis E/MainActivity.java: CameraInstance: null
12-16 19:54:08.352 13572-13572/de.aintlarry.shotanalysis E/MainActivity.java: CameraInstance: null
12-16 19:54:08.430 13572-13621/de.aintlarry.shotanalysis I/Adreno: QUALCOMM build : 8e7809e, Ic2ca81afa7
Build Date : 09/18/15
OpenGL ES Shader Compiler Version: XE031.05.13.02
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_PRIVATE_REDFOX64_MBR_RB1.05.01.01.003.020
Remote Branch : NONE
Reconstruct Branch : NOTHING
12-16 19:54:08.481 13572-13572/de.aintlarry.shotanalysis E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.aintlarry.shotanalysis, PID: 13572
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewDisplay(android.view.SurfaceHolder)' on a null object reference
at de.aintlarry.shotanalysis.CameraPreview.surfaceCreated(CameraPreview.java:33)
at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2140)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1162)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6229)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
at android.view.Choreographer.doCallbacks(Choreographer.java:676)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5781)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
12-16 19:54:10.562 13572-13572/de.aintlarry.shotanalysis D/Process: killProcess, pid=13572
12-16 19:54:10.571 13572-13572/de.aintlarry.shotanalysis D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:113 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:69


有人知道这个问题吗?我尝试了所有发现的建议。

最佳答案

请改变这个

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) {   //This catch block does not handle NullPointers
        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    }
}


对此

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 (Exception e) { //This catch block does handle NullPointers
        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    }
}


您使用的catch块太窄,无法捕获mCamera对象为null。这样可以防止崩溃,但是您还应该检查mCamera为什么在那里为null,以及是否可以/应该为null。但是,完全有可能它尚未完全初始化,将在以后的调用中:-)

08-16 15:10