App crashes with NullPointerException on Android
See original GitHub issueDescribe 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)
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Hi, @Lyokone, unfortunately the problem isn’t solved, could you please look at new PR request #314
Great ! Was kinda obvious, but just wanted to be sure that you weren’t stuck 😉