Search code examples
androidkotlincameraruntimeexception

RuntimeException: startPreview failed


Get next error in crashlytics:

Fatal Exception: java.lang.RuntimeException: startPreview failed
       at android.hardware.Camera.startPreview(Camera.java)
       at com.google.android.gms.vision.CameraSource.start(CameraSource.java:24)
       at com.sai.authenticator.features.qr.QrScannerActivity.startCameraSource(QrScannerActivity.java:174)
       at com.sai.authenticator.features.qr.QrScannerActivity.access$onReceivedCodes(QrScannerActivity.java:54)
       at com.sai.authenticator.features.qr.QrScannerActivity$setupViewModel$2.onChanged(QrScannerActivity.java:100)
       at com.sai.authenticator.features.qr.QrScannerActivity$setupViewModel$2.onChanged(QrScannerActivity.java:54)
       at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
       at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
       at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
       at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
       at androidx.lifecycle.LiveData$1.run(LiveData.java:91)
       at android.os.Handler.handleCallback(Handler.java:873)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7081)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

Here is my code QrScannerActivity.java:174:

enter image description here

It seems like everything is wrapped up and safe, but the error fell out.


Solution

  • Unfortunately, I can't reproduce the error, so I assumed that the surface could be null and therefore the application crashes at startup. That's why I decided to update the code this way.

    val holder = surfaceView?.holder
    if (holder != null) cameraSource?.start(holder)
    else [email protected](textResId = R.string.errors_failed_to_start_camera)