journeyapps / zxing-android-embedded

Barcode scanner library for Android, based on the ZXing decoder
https://journeyapps.com/
Apache License 2.0
5.77k stars 1.27k forks source link

Inside Fragment - Fatal Exception: java.lang.IllegalStateException: Attempting to launch an unregistered ActivityResultLauncher with contract com.journeyapps.barcodescanner.ScanContract@4d9510d and input com.journeyapps.barcodescanner.ScanOptions@447a8c2. You must ensure the ActivityResultLauncher is registered before calling launch(). #720

Open MuhammadAjmalZia opened 2 years ago

MuhammadAjmalZia commented 2 years ago

Description of the problem:

Facing this crash on few devices. We're using scanner inside Fragment and the launcher is initialise on the class level like below:

private val barcodeLauncher = registerForActivityResult(
    ScanContract()
) { result: ScanIntentResult ->
    if (!result.contents.isNullOrEmpty()) {
        isQRScanned = true
        validateGiftCard(result.contents)
    }
}

And on button click action we're calling it like below:

private fun launchScanner() { isQRScanned = false val options = ScanOptions() options.setDesiredBarcodeFormats(ScanOptions.QR_CODE) options.setPrompt("Scan a barcode or QR Code") options.setCameraId(0) options.setOrientationLocked(true) options.setBeepEnabled(false) options.setBarcodeImageEnabled(true) barcodeLauncher.launch(options) }

But facing this crash on some devices in the live App - We're not able to reproduce it on our devices:

Fatal Exception: java.lang.IllegalStateException: Attempting to launch an unregistered ActivityResultLauncher with contract com.journeyapps.barcodescanner.ScanContract@4d9510d and input com.journeyapps.barcodescanner.ScanOptions@447a8c2. You must ensure the ActivityResultLauncher is registered before calling launch(). at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:168) at androidx.fragment.app.Fragment$10.launch(Fragment.java:3616) at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:47) at ae.etisalat.smiles.kt.presentation.smilesubscription.subscribesubscription.SmilesSubscriptionFragment.launchScanner(SmilesSubscriptionFragment.kt:243) at ae.etisalat.smiles.kt.presentation.smilesubscription.subscribesubscription.SmilesSubscriptionFragment.initUI$lambda-5(SmilesSubscriptionFragment.kt:229) at ae.etisalat.smiles.kt.presentation.smilesubscription.subscribesubscription.SmilesSubscriptionFragment.$r8$lambda$JXL3kFSxrqsuGf81Wbw_3D-rZBw(SmilesSubscriptionFragment.kt) at ae.etisalat.smiles.kt.presentation.smilesubscription.subscribesubscription.SmilesSubscriptionFragment$$InternalSyntheticLambda$0$a6a1f6be84fe10956cf050fb962ba033073d76d475a021e2c9452c20251158c9$1.onClick(SmilesSubscriptionFragment.java:2) at android.view.View.performClick(View.java:7281) at android.view.View.performClickInternal(View.java:7255) at android.view.View.access$3600(View.java:828) at android.view.View$PerformClick.run(View.java:27924) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8387) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)

Which library version are you using? E.g. 3.2.0. implementation('com.journeyapps:zxing-android-embedded:4.3.0') { transitive = false } implementation 'com.google.zxing:core:3.5.0'

Which phone/tablet are you using, and which Android version does it run? (e.g. Samsung Galaxy S5, Android 5.0) Devices starting from Android 10 upto Android 12

aDoes the same happen on other devices or an emulator? Not able to reproduce.

Can you reproduce the issue in the sample project included with the library? If not, can you provide your own sample project or sample code that produces this error? Not able to reproduce.

In the case of an error do you have a stack trace or adb logs?

remain4life commented 2 years ago

I ran into the same problem and found this post after searching for a solution to this problem. I can say for sure that I had this problem when migrating to SDK 33 and updating androidx.appcompat:appcompat library to version 1.5.0 and 1.5.1; on version 1.4.2 and SDK 31 this problem did not arise. This crash has nothing to do with Zxing library.

chaiyaphat20 commented 1 year ago
  1. private lateinit var barCodeQrLauncher: ActivityResultLauncher
  2. override fun onViewCreated(view: View, savedInstanceState: Bundle?) { .... barCodeQrLauncher = registerForActivityResult( ScanContract() ) { result: ScanIntentResult -> if (result.contents != null) { Toast.makeText(requireContext(), result.contents, Toast.LENGTH_LONG).show() } } }

3. override fun onDestroy() { super.onDestroy() barCodeQrLauncher.unregister() //<<<< }

ref. https://kaustubhpatange.github.io/blog/post/kotlin-activity-result/