android / camera-samples

Multiple samples showing the best practices in camera APIs on Android.
Apache License 2.0
4.95k stars 2.32k forks source link

Camera2.Basic crashes when press button #570

Open stevebroshar opened 7 months ago

stevebroshar commented 7 months ago

Please follow the following instructions before filing a bug:

  1. Github issues under this project are only for sample-related issues. If you have modified the sample code in any way, then this is probably not the appropriate place to report a bug.
  2. If you are trying to report a bug related to the CameraX library then you can do so at the Android Issue Tracker: https://issuetracker.google.com/components/618491.
  3. Include the specific sample where you found the issue, in addition to device model and specific Android build number -- which can be found under Settings > About phone.
  4. Include error logs from logcat, if there are any.
  5. If you can, also attach a bug report-- but please keep in mind that personal information can sometimes be found in those, so double check before attaching.

Target: PIxel 7 API 34 simulator Android Studio: 2023.1.1 Clicking button in Camera2Basic (to take pic?) crashes app.

2024-01-16 14:52:38.113 10412-10412 AndroidRuntime com.android.example.camera2.basic E FATAL EXCEPTION: main Process: com.android.example.camera2.basic, PID: 10412 java.lang.NullPointerException at com.example.android.camera2.basic.fragments.CameraFragment.getFragmentCameraBinding(CameraFragment.kt:79) at com.example.android.camera2.basic.fragments.CameraFragment.access$getFragmentCameraBinding(CameraFragment.kt:74) at com.example.android.camera2.basic.fragments.CameraFragment$animationTask$2.invoke$lambda-1$lambda-0(CameraFragment.kt:117) at com.example.android.camera2.basic.fragments.CameraFragment$animationTask$2.$r8$lambda$kSaIYQrPru2VxvpYHL5tPwU4w3s(Unknown Source:0) at com.example.android.camera2.basic.fragments.CameraFragment$animationTask$2$$ExternalSyntheticLambda1.run(Unknown Source:2) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) at android.app.ActivityThread.main(ActivityThread.java:8177) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

stevebroshar commented 7 months ago

I think the issue is the call to property fragmentCameraBinding via overlay.postDelayed. The property is defined as _fragmentCameraBinding!!, but in the delayed call sometimes the value is null -- probably due to clear in onDestroyView. I think a better implementation is using a lateinit field and no property:

private lateinit var fragmentCameraBinding: FragmentCameraBinding

I think the sweet spot for lateinit is exactly this scenario: a variable that is basically immutable but initialized after ctor.