Android CameraX物理设备二次启动Intent崩溃,模拟器正常
CameraX二次启动相机Intent物理设备崩溃问题解决
问题场景
我通过自定义CustomCameraActivity和CustomCamera类实现了CameraX功能,使用pictureActivityResultLauncher启动相机Intent。该功能在Android模拟器上运行完全正常,但在物理设备上,首次拍照操作正常,二次启动相机Intent时会直接崩溃。
错误日志
崩溃时抛出空指针异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.Display.getRotation()' on a null object reference
排查过程
- 尝试手动释放
cameraProvider,但问题依然存在 - 跟踪代码后确认,崩溃根源来自
bindCameraUseCases方法中的setTargetRotation调用
解决方法
移除bindCameraUseCases方法里的setTargetRotation相关代码后,二次启动相机Intent的崩溃问题彻底解决。
内容的提问来源于stack exchange,提问作者정민재
相关产品推荐
相关产品推荐

