Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 11 tasks
LoicUV opened this issue Jun 24, 2024 · 4 comments
Open
5 of 11 tasks

Comments

@LoicUV
Copy link

LoicUV commented Jun 24, 2024

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.
@robingenz
Copy link
Member

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 robingenz added the platform: android Android platform label Jun 24, 2024
@emmaB93

This comment has been minimized.

@emmaB93

This comment has been minimized.

@iBobik
Copy link

iBobik commented Oct 26, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants