capawesome-team / capacitor-mlkit

⚡️ ML Kit plugins for Capacitor. Supports Android and iOS.
https://capawesome.io/plugins/mlkit/
Apache License 2.0
147 stars 46 forks source link

bug: `NullPointerException` during `startScan(...)` with capacitor inside an Android fragment #160

Open LoicUV opened 5 months ago

LoicUV commented 5 months ago

Plugin(s)

Version

6.1.0

Platform(s)

Current behavior

Hi there.

So I have kind of a specific android app structure with multiple android fragments, and with capacitor instanciated in one of them using BridgeFragment class. The particularity is that plugins are not automatically loaded when using this class, you have to manually add each plugin inside the fragment class as stated here https://github.com/ionic-team/capacitor/issues/5564#issuecomment-1735320796. This works well, except for the barcode scanning plugin, which produces the famous NullPointerException during startScan(...) despite having data binding enabled.

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.camera.view.PreviewView.setScaleType(androidx.camera.view.PreviewView$ScaleType)' on a null object reference
    at io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.BarcodeScanner.lambda$startScan$0(BarcodeScanner.java:106)

My guess is that the following code breaks because the app doesn't find the preview view since the plugin is instanciated in a fragment ?

// BarcodeScanner.java:106
previewView = plugin.getActivity().findViewById(R.id.preview_view);

I'm a beginner android developer so I'm kind of lost here as to where the problem lies exactly. I'm aware that my app's implementation with fragment is rather advanced so I'll understand if it's not something you want to support in your plugin.

Expected behavior

No error during scan.

Reproduction

https://github.com/LoicUV/scanner-example

Steps to reproduce

  1. Build & run the app
  2. Click the SCAN BARCODE button
  3. Check LogCat for the error

Other information

No response

Capacitor doctor

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 6.1.0
  @capacitor/core: 6.1.0
  @capacitor/android: 6.1.0
  @capacitor/ios: 6.1.0

Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/cli: 6.1.0
  @capacitor/android: 6.1.0
  @capacitor/core: 6.1.0

[success] Android looking great! 👌

Before submitting

robingenz commented 5 months ago

Thank you for your request. Currently I do not plan to invest time in this issue as it is a very specific case but PRs are welcome. I leave this open for now.

emmaB93 commented 4 months ago

@robingenz I'm having the same issue but in a normal Ionic/Capacitor App. It worked but now I suddenly get this error...

VM3:943 Uncaught (in promise) Error: Attempt to invoke virtual method 'void androidx.camera.view.PreviewView.setScaleType(androidx.camera.view.PreviewView$ScaleType)' on a null object reference

`const startScan = async () => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { camera } = await BarcodeScanner.checkPermissions(); document.querySelector('body')?.classList.add('barcode-scanner-active');

    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const listener = await BarcodeScanner.addListener(
        'barcodeScanned',
        async result => {
            BarcodeScanner.stopScan();
            setShowQrLoading(true);
            document.querySelector('body')?.classList.remove('barcode-scanner-active');
            await loginWithToken(result.barcode.rawValue);
        },
    );

    // Start the barcode scanner
    await BarcodeScanner.startScan();
};`

After the startScan() method the error appears...
emmaB93 commented 4 months ago

@robingenz I found the issue:

https://github.com/capawesome-team/capacitor-mlkit/tree/main/packages/barcode-scanning#data-binding

Data binding was not enabled...

iBobik commented 1 month ago

Thank you @emmaB93 for your comment about another cause of this error - not enabled data binding.

Sadly I found it after few hours first seeing this issue where I landed searching the error message. Then I investigated how it could happened in my project where I have not intentionally messed with Android fragments. Then after many hopeless feelings I randomly found your minimized comments. 🤦‍♂️