juliuscanute / qr_code_scanner

QR Code Scanner for Flutter
BSD 2-Clause "Simplified" License
1.01k stars 782 forks source link

[BUG] NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.isUiContext()' after background NFC tag scan #320

Open AndrewBotalov80 opened 3 years ago

AndrewBotalov80 commented 3 years ago

How to reproduce:

  1. Start application
  2. Open screen with QR scan
  3. Go to home (app in background)
  4. Scan NFC tag
  5. Return to app and open QR scan screen
  6. Get grey screen

We can skip step 2 and not open QR scan before, anyway after NFC scan and returning to app we will have grey screen.

If we scan NFC in foreground everything is OK.

Screenshots: Step 2. IMAGE 2021-05-31 12:25:41 Step 5. IMAGE 2021-05-31 12:26:00

Video: https://user-images.githubusercontent.com/18752756/120157065-0db08380-c20c-11eb-8059-8b7a0b31b100.mp4

Flutter information flutter doctor -v

[✓] Flutter (Channel stable, 2.0.6, on macOS 11.3.1 20E241 darwin-x64, locale ru-RU)
    • Flutter version 2.0.6 at /Users/dushes/Documents/development/flutter
    • Framework revision 1d9032c7e1 (4 weeks ago), 2021-04-29 17:37:58 -0700
    • Engine revision 05e680e202
    • Dart version 2.12.3

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/dushes/Documents/development/android/sdk
    • Platform android-30, build-tools 30.0.1
    • ANDROID_HOME = /Users/dushes/Documents/development/android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 46.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.56.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.22.0

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212

Device (please complete the following information):

Additional context pubspec.yaml:

environment:
  sdk: ">=2.12.0-0 <3.0.0"
qr_code_scanner: ^0.4.0

Found error after debug:

D/OpScreenModeManager(16181): setRefreshRate view io.flutter.embedding.android.FlutterSurfaceView{86b1f1f V.E...... ........ 0,0-1080,2340} viewRate 1 width 1080 height 2340
D/DecorView(16181): onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@8228035[MainActivity]
D/ViewRootImpl[MainActivity](16181): windowFocusChanged hasFocus=true inTouchMode=true
E/sqlite3_android(16181): [IKR-38846] ONEPLUS_NAME_PARTS_MATCH SQLITE_OK
W/DisplayEventDispatcher(16181): dispatcher 0x78e3daf8d0 ~ ignoring unknown event type 0x6d746f6e
W/DisplayEventDispatcher(16181): dispatcher 0x7893dd2bb0 ~ ignoring unknown event type 0x6d746f6e
I/flutter (16181): Available block height = 193.85714285714286
I/tparkmobile.de(16181): failed to find target package for overlay /system/product/overlay/ProductOverlay-oneplus-framework-res.apk
I/tparkmobile.de(16181): failed to find target package for overlay /system/system_ext/overlay/CommonOverlay-oneplus-framework-res.apk
I/tparkmobile.de(16181): failed to find target package for overlay /odm/overlay/OdmOverlay-oneplus-framework-res.apk
E/MethodChannel#flutter/platform_views(16181): Failed to handle method call
E/MethodChannel#flutter/platform_views(16181): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.isUiContext()' on a null object reference
E/MethodChannel#flutter/platform_views(16181):  at android.view.ViewConfiguration.get(ViewConfiguration.java:507)
E/MethodChannel#flutter/platform_views(16181):  at android.view.View.<init>(View.java:5356)
E/MethodChannel#flutter/platform_views(16181):  at android.view.View.<init>(View.java:5518)
E/MethodChannel#flutter/platform_views(16181):  at android.view.ViewGroup.<init>(ViewGroup.java:708)
E/MethodChannel#flutter/platform_views(16181):  at android.view.ViewGroup.<init>(ViewGroup.java:704)
E/MethodChannel#flutter/platform_views(16181):  at android.view.ViewGroup.<init>(ViewGroup.java:700)
E/MethodChannel#flutter/platform_views(16181):  at android.view.ViewGroup.<init>(ViewGroup.java:696)
E/MethodChannel#flutter/platform_views(16181):  at com.journeyapps.barcodescanner.CameraPreview.<init>(CameraPreview.java:228)
E/MethodChannel#flutter/platform_views(16181):  at com.journeyapps.barcodescanner.BarcodeView.<init>(BarcodeView.java:76)
E/MethodChannel#flutter/platform_views(16181):  at net.touchcapture.qr.flutterqr.QRView.initBarCodeView(QRView.kt:201)
E/MethodChannel#flutter/platform_views(16181):  at net.touchcapture.qr.flutterqr.QRView.getView(QRView.kt:196)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.plugin.platform.SingleViewPresentation.onCreate(SingleViewPresentation.java:189)
E/MethodChannel#flutter/platform_views(16181):  at android.app.Dialog.dispatchOnCreate(Dialog.java:419)
E/MethodChannel#flutter/platform_views(16181):  at android.app.Dialog.show(Dialog.java:313)
E/MethodChannel#flutter/platform_views(16181):  at android.app.Presentation.show(Presentation.java:257)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.plugin.platform.VirtualDisplayController.<init>(VirtualDisplayController.java:95)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.plugin.platform.VirtualDisplayController.create(VirtualDisplayController.java:48)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.plugin.platform.PlatformViewsController$1.createVirtualDisplayForPlatformView(PlatformViewsController.java:207)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:104)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:59)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#flutter/platform_views(16181):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
E/MethodChannel#flutter/platform_views(16181):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#flutter/platform_views(16181):  at android.os.MessageQueue.next(MessageQueue.java:335)
E/MethodChannel#flutter/platform_views(16181):  at android.os.Looper.loop(Looper.java:183)
E/MethodChannel#flutter/platform_views(16181):  at android.app.ActivityThread.main(ActivityThread.java:8010)
E/MethodChannel#flutter/platform_views(16181):  at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter/platform_views(16181):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
E/MethodChannel#flutter/platform_views(16181):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
I/flutter (16181): Caught error: PlatformException(error, Attempt to invoke virtual method 'boolean android.content.Context.isUiContext()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.isUiContext()' on a null object reference
I/flutter (16181):  at android.view.ViewConfiguration.get(ViewConfiguration.java:507)
I/flutter (16181):  at android.view.View.<init>(View.java:5356)
I/flutter (16181):  at android.view.View.<init>(View.java:5518)
I/flutter (16181):  at android.view.ViewGroup.<init>(ViewGroup.java:708)
I/flutter (16181):  at android.view.ViewGroup.<init>(ViewGroup.java:704)
I/flutter (16181):  at android.view.ViewGroup.<init>(ViewGroup.java:700)
I/flutter (16181):  at android.view.ViewGroup.<init>(ViewGroup.java:696)
I/flutter (16181):  at com.journeyapps.barcodescanner.CameraPreview.<init>(CameraPreview.java:228)
I/flutter (16181):  at com.journeyapps.barcodescanner.BarcodeView.<init>(BarcodeView.java:76)
I/flutter (16181):  at net.touchcapture.qr.flutterqr.QRView.initBarCodeView(QRView.kt:201)
I/flutter (16181):  at net.touchcapture.qr.flutterqr.QRView.getView(QRView.kt:196)
I/flutter (16181):  at io.flutter.plugin.platform.SingleViewPresentation.onCreate(Si

My project code:

class AccessCheckScreenModel extends BaseModel {
// ***** QR *****
  Barcode? result;
  QRViewController? controller;
  var qrKey = GlobalKey(debugLabel: 'QR');
  // **************
...
}
class _AccessCheckScreenState extends TPLifecycleWatchState<AccessCheckScreen>
    with AfterLayoutMixin<AccessCheckScreen> {
  late AccessCheckScreenModel model;

  @override
  void initState() {
    model = AccessCheckScreenModel(widget.args);
    model.setState(BaseScreenState.IDLE);
    super.initState();
  }

  @override
  void afterFirstLayout(BuildContext context) {
    model.fetchData();
  }

// In order to get hot reload to work we need to pause the camera if the platform
  // is android, or resume the camera if the platform is iOS.
  @override
  void reassemble() {
    super.reassemble();
    if (isAndroid) {
      model.controller?.pauseCamera();
    }
    model.controller?.resumeCamera();
  }

@override
  void dispose() {
    model.controller?.dispose();
    super.dispose();
  }

Widget _buildQrView(BuildContext context) {
    final size = MediaQuery.of(context).size;
    // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
    final scanArea = size.width / 1.25;

    final flashActive = model.controller?.getFlashStatus() ?? false;

    final elementsColor = Colors.white;

    // To ensure the Scanner view is properly sizes after rotation
    // we need to listen for Flutter SizeChanged notification and update controller
    return LayoutBuilder(
      builder: (context2, constraints) {
        final blockHeight = (constraints.maxHeight - scanArea) / 2;
        print('Available block height = $blockHeight');
        return Stack(
          children: [
            Container(
              child: QRView(
                key: model.qrKey,
                cameraFacing: CameraFacing.back,
                onQRViewCreated: _onQRViewCreated,
                formatsAllowed: [BarcodeFormat.qrcode],
                overlay: QrScannerOverlayShape(
                  borderColor: Theme.of(context).accentColor,
                  borderRadius: 10,
                  borderLength: 30,
                  borderWidth: 10,
                  cutOutSize: scanArea,
                ),
              ),
            ),
...
}

void _onQRViewCreated(QRViewController controller) {
    model.setController(controller);
    controller.scannedDataStream.listen((scanData) {
      model.setResult(scanData.code);
    });
  }

@override
  void onPaused() {
    model.logger.info(
      'On paused',
      save: true,
    );
    model.pauseCamera();
  }

  @override
  void onResumed() {
    model.logger.info(
      'On resumed',
      save: true,
    );
    model.resumeCamera();
  }
AndrewBotalov80 commented 3 years ago

Found some error

`E/MethodChannel#flutter/platform_views(30986): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.isUiContext()' on a null object reference
E/MethodChannel#flutter/platform_views(30986):  at android.view.ViewConfiguration.get(ViewConfiguration.java:507)
E/MethodChannel#flutter/platform_views(30986):  at android.view.View.<init>(View.java:5356)
E/MethodChannel#flutter/platform_views(30986):  at android.view.View.<init>(View.java:5518)
E/MethodChannel#flutter/platform_views(30986):  at android.view.ViewGroup.<init>(ViewGroup.java:708)
E/MethodChannel#flutter/platform_views(30986):  at android.view.ViewGroup.<init>(ViewGroup.java:704)
E/MethodChannel#flutter/platform_views(30986):  at android.view.ViewGroup.<init>(ViewGroup.java:700)
E/MethodChannel#flutter/platform_views(30986):  at android.view.ViewGroup.<init>(ViewGroup.java:696)
E/MethodChannel#flutter/platform_views(30986):  at com.journeyapps.barcodescanner.CameraPreview.<init>(CameraPreview.java:228)
E/MethodChannel#flutter/platform_views(30986):  at com.journeyapps.barcodescanner.BarcodeView.<init>(BarcodeView.java:76)
E/MethodChannel#flutter/platform_views(30986):  at net.touchcapture.qr.flutterqr.QRView.initBarCodeView(QRView.kt:201)
E/MethodChannel#flutter/platform_views(30986):  at net.touchcapture.qr.flutterqr.QRView.getView(QRView.kt:196)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.plugin.platform.SingleViewPresentation.onCreate(SingleViewPresentation.java:189)
E/MethodChannel#flutter/platform_views(30986):  at android.app.Dialog.dispatchOnCreate(Dialog.java:419)
E/MethodChannel#flutter/platform_views(30986):  at android.app.Dialog.show(Dialog.java:313)
E/MethodChannel#flutter/platform_views(30986):  at android.app.Presentation.show(Presentation.java:257)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.plugin.platform.VirtualDisplayController.<init>(VirtualDisplayController.java:95)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.plugin.platform.VirtualDisplayController.create(VirtualDisplayController.java:48)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.plugin.platform.PlatformViewsController$1.createVirtualDisplayForPlatformView(PlatformViewsController.java:207)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:104)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:59)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#flutter/platform_views(30986):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
E/MethodChannel#flutter/platform_views(30986):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#flutter/platform_views(30986):  at android.os.MessageQueue.next(MessageQueue.java:335)
E/MethodChannel#flutter/platform_views(30986):  at android.os.Looper.loop(Looper.java:183)
E/MethodChannel#flutter/platform_views(30986):  at android.app.ActivityThread.main(ActivityThread.java:8010)
E/MethodChannel#flutter/platform_views(30986):  at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter/platform_views(30986):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
E/MethodChannel#flutter/platform_views(30986):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
masaid3000 commented 3 years ago

yes, I also get an error message like that, after I restart the application using the FlutterRestart.restartApp() method, then immediately open the QR Scanner.