尝试在空对象引用上写入字段'android.hardware.camera2.ICameraDeviceUser

时间:2016-05-24 11:31:53

标签: android nullpointerexception android-camera android-camera2

我使用Camera2为具有API>的设备实现相机预览20.我在android 5.0.0上遇到了asus_t00j的错误。我不确定为什么会这样。

Fatal Exception: java.lang.NullPointerException: Attempt to write to field 'android.hardware.camera2.ICameraDeviceUser android.hardware.camera2.impl.CameraDeviceImpl.mRemoteDevice' on a null object reference
   at android.hardware.camera2.impl.CameraDeviceImpl.close(CameraDeviceImpl.java:773)
   at com.vyda.stream.screencast.CameraHelper.closeCamera(CameraHelper.java:410)
   at com.vyda.stream.screencast.CameraHelper.stopCameraAndRemoveWindow(CameraHelper.java:150)
   at com.vyda.stream.screencast.ScreencastService.endScreencasting(ScreencastService.java:307)
   at com.vyda.stream.screencast.ScreencastService$8.onClick(ScreencastService.java:275)
   at android.view.View.performClick(View.java:4756)
   at android.view.View$PerformClick.run(View.java:19761)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5253)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

CameraHelper :: closeCamera()

private void closeCamera() {
        try {
            cameraOpenCloseLock.acquire();
            if (null != captureSession) {
                captureSession.close();
                captureSession = null;
            }
            if (null != cameraDevice) {
                cameraDevice.close();
                cameraDevice = null;
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
        } finally {
            cameraOpenCloseLock.release();
        }
    }

1 个答案:

答案 0 :(得分:2)

captureSession.close()是异步的,这意味着当您的代码到达cameraDevice.close()时,底层的相机对象(但不是cameraDevice成员 CameraHelper )被毁了。同时使用captureSession.close()cameraDevice.close(),您应该等待onClosed()回调。

cameraDevice.close()的说明解释了活动会话将为您关闭,因此您不需要在您的方案中进行captureSession.close()调用。

PS我相信你遇到的是华硕设备上的Camera2 API的错误实现。