Flutterlocation: App crashes with NullPointerException on Android

Created on 2 Mar 2020  路  7Comments  路  Source: Lyokone/flutterlocation

Describe the bug
Here is some information: I'm using _geofencing_ and _location_ plugins in the app and app's Application class looks like that:

...
class Application : FlutterApplication(), PluginRegistrantCallback {
 ...
    override fun registerWith(registry: PluginRegistry) {
        GeneratedPluginRegistrant.registerWith(registry)
    }
...
}
...

So, what's the issue: when geofence event (user enters or exits specified area) is caught a method Application.registerWith(...) is called but Registrar doesn't contain reference to the host activity for the _location_ plugin. That's why FlutterLocation.setActivity() is called with nullable activity and
I see that this method is expecting that it might happen:

...
 void setActivity(@Nullable Activity activity) {
        this.activity = activity;
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(activity); 
...
    }
...

But LocationServices.getFusedLocationProviderClient(activity); expects non null activity and it throws a NullPointerException then application crashes.
Could you please add try-catch block to handle this error ?

...
 void setActivity(@Nullable Activity activity) {
       try{
        this.activity = activity;
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(activity); 
...
       } catch(Exception e){
        e.printStackTrace();
       }
    }
...

Please this is an emergency!

Tested on:

  • Android, API Level 28 and higher

Other plugins:

  • geofencing
  • place_picker

Additional logs

E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to create service io.flutter.plugins.geofencing.GeofencingService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.app.Activity.getMainLooper()' on a null object reference
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3965)
        at android.app.ActivityThread.access$1500(ActivityThread.java:219)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        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)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.app.Activity.getMainLooper()' on a null object reference
        at com.google.android.gms.common.api.GoogleApi.<init>(Unknown Source:50)
        at com.google.android.gms.location.FusedLocationProviderClient.<init>(Unknown Source:8)
        at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:2)
        at com.lyokone.location.FlutterLocation.setActivity(FlutterLocation.java:87)
        at com.lyokone.location.LocationPlugin.registerWith(LocationPlugin.java:36)
        at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:40)
        at com.example.app.app.Application.registerWith(Application.kt:18)
        at io.flutter.plugins.geofencing.GeofencingService.startGeofencingService(GeofencingService.kt:74)
        at io.flutter.plugins.geofencing.GeofencingService.onCreate(GeofencingService.kt:114)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3953)
        at android.app.ActivityThread.access$1500(ActivityThread.java:219)聽
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)聽
        at android.os.Handler.dispatchMessage(Handler.java:107)聽
        at android.os.Looper.loop(Looper.java:214)聽
        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)聽
bug

Most helpful comment

Hi, @Lyokone, unfortunately the problem isn't solved, could you please look at new PR request #314

All 7 comments

Hey ! I'll try to fix this as soon as possible.
You can tho point to your branch in pubspec to use your changes right away :)

Yeah, but the problem is that I'm using _place_picker_ plugin that uses _location_ one :)

Hum I guess you could fork place_picker and change the pubspec ?
But I'll try to fix this asap

Sure, I've already done it
Thank you, I really appreciate it
Anyway I can change files due to your comments

Great ! Was kinda obvious, but just wanted to be sure that you weren't stuck ;)

310 Is closing this.

Hi, @Lyokone, unfortunately the problem isn't solved, could you please look at new PR request #314

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LinusU picture LinusU  路  3Comments

lehno picture lehno  路  5Comments

rsproule picture rsproule  路  13Comments

KingWu picture KingWu  路  11Comments

fre391 picture fre391  路  4Comments