Skip to content

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

@LoicUV

Description

@LoicUV

Plugin(s)

  • Barcode Scanning
  • Face Detection
  • Face Mesh Detection
  • Selfie Segmentation
  • Translation

Version

6.1.0

Platform(s)

  • 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 ionic-team/capacitor#5564 (comment).
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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions