Android相机:取消装箱'characteristic.get(CameraCharacteristics.LENS_FACING)'可能会产生NPE

时间:2015-12-21 18:50:28

标签: java android android-camera2

我正在实施Camera2,当我设置相机输出时,我收到以下警告(我无法重现任何可能出现的“npe”)(我得到了我的来自google样本的代码)以下行给出了以下警告:

  

“取消装箱'features.get(CameraCharacteristics.LENS_FACING)'   可能会产生NPE。“

 for (String cameraId : manager.getCameraIdList()) {
      CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

      if (characteristics.get(CameraCharacteristics.LENS_FACING) 
           == CameraCharacteristics.LENS_FACING_FRONT) { //THESE LINE IS WHERE THE WARNING APPEARS
           continue;
         }
   ....

有人知道如何以适当的方式检查这个以便在发生这种情况时捕获它吗?

1 个答案:

答案 0 :(得分:1)

您正在将它与原始int进行比较,characteristics.get(CameraCharacteristics.LENS_FACING)可能会返回Integer这是一个对象。会发生什么事情,你将拥有

Integer integer = characteristics.get(CameraCharacteristics.LENS_FACING);
if (integer.intValue() == CameraCharacteristics.LENS_FACING_FRONT) {

}

如果integer为空,您将获得NPE访问intValue()