Skip to content

NPE when running in a background service #17

@ened

Description

@ened

🔙 Regression

The plugin shows a error when being started in a background service.
This seems to be related to the V2 embedding.

Old (and correct) behavior

No crash on V1 embedding.

Current behavior

2020-03-31 22:30:26.265 15821-15821/com.mynextbase.connect E/MethodChannel#flutter.baseflow.com/google_api_availability/methods: Failed to handle method call
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at com.google.android.gms.common.GooglePlayServicesUtilLight.isGooglePlayServicesAvailable(Unknown Source:12)
        at com.google.android.gms.common.GoogleApiAvailabilityLight.isGooglePlayServicesAvailable(Unknown Source:5)
        at com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(com.google.android.gms:play-services-base@@17.1.0:96)
        at com.google.android.gms.common.GoogleApiAvailabilityLight.isGooglePlayServicesAvailable(Unknown Source:3)
        at com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(com.google.android.gms:play-services-base@@17.1.0:95)
        at com.baseflow.googleapiavailability.GoogleApiAvailabilityPlugin.onMethodCall(GoogleApiAvailabilityPlugin.java:105)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:226)
        at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
        at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:631)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:336)
        at android.os.Looper.loop(Looper.java:174)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Reproduction steps

Run the plugin in a background service.

Configuration

Version: 2.0.3

Platform:

  • 📱 iOS
  • 🤖 Android

The implementation of the V2 embedding seems a little complicated.
The applicationContext should always be available to the GoogleApiAvailabilityPlugin class, and "activity" could be set depending on whether the activity has been connected or not.

The check

      if (context instanceof Activity) {
        Activity activity = (Activity) context;
        if (showDialog != null && showDialog) {
          googleApiAvailability.showErrorDialogFragment(activity, connectionResult, REQUEST_GOOGLE_PLAY_SERVICES);
        }
      }

could then be replaced by "if (activity != null)".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions