capacitor-mlkit icon indicating copy to clipboard operation
capacitor-mlkit copied to clipboard

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

Open LoicUV opened this issue 1 year ago • 7 comments

Plugin(s)

  • [X] Barcode Scanning
  • [ ] Face Detection
  • [ ] Face Mesh Detection
  • [ ] Selfie Segmentation
  • [ ] Translation

Version

6.1.0

Platform(s)

  • [X] Android
  • [ ] iOS
  • [ ] Web

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

  • [X] I have read and followed the bug report guidelines.
  • [X] I have attached links to possibly related issues and discussions.
  • [X] I understand that incomplete issues (e.g. without reproduction) are closed.

LoicUV avatar Jun 24 '24 09:06 LoicUV

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.

robingenz avatar Jun 24 '24 09:06 robingenz

@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 avatar Jul 10 '24 15:07 emmaB93

@robingenz I found the issue:

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

Data binding was not enabled...

emmaB93 avatar Jul 10 '24 16:07 emmaB93

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. 🤦‍♂️

iBobik avatar Oct 26 '24 17:10 iBobik

I'm also running into this issue. @iBobik @emmaB93 what do you mean by enabling data binding? The link you provided no longer works

RickGroenewegen avatar Jun 01 '25 14:06 RickGroenewegen

I'm also running into this issue. @iBobik @emmaB93 what do you mean by enabling data binding? The link you provided no longer works

See this older version. I don’t know why they removed it. https://github.com/capawesome-team/capacitor-mlkit/blob/fa76d3c0eafa1c60143421caa04f3c553401c87f/packages/barcode-scanning/README.md#data-binding

iBobik avatar Jun 01 '25 16:06 iBobik

Data binding is no longer needed in the latest version, see https://github.com/capawesome-team/capacitor-mlkit/pull/233/commits/df30d45f4c21c70dab803e54ad0c8f1e1733e158.

robingenz avatar Jun 01 '25 17:06 robingenz