Camera2.Basic crashes when press button
stevebroshar opened this issue · 1 comments
Please follow the following instructions before filing a bug:
- 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.
- 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.
- 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.
- Include error logs from logcat, if there are any.
- 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)
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.