mauron85 / react-native-background-geolocation

Background and foreground geolocation plugin for React Native. Tracks user when app is running in background.
Apache License 2.0
1.33k stars 559 forks source link

invalid null provider error on start #561

Open andkom opened 2 years ago

andkom commented 2 years ago

Your Environment

Context

In firebase console I can see crash events with error:

Fatal Exception: java.lang.RuntimeException: Unable to start service com.marianhello.bgloc.service.LocationServiceImpl@ee59593 with null: java.lang.IllegalArgumentException: invalid null provider
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4794)
       at android.app.ActivityThread.access$2000(ActivityThread.java:301)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2196)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:246)
       at android.app.ActivityThread.main(ActivityThread.java:8595)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by java.lang.IllegalArgumentException: invalid null provider
       at com.android.internal.util.Preconditions.checkArgument(Preconditions.java:52)
       at android.location.LocationManager.requestLocationUpdates(LocationManager.java:927)
       at com.marianhello.bgloc.provider.RawLocationProvider.onStart(RawLocationProvider.java:49)
       at com.marianhello.bgloc.service.LocationServiceImpl$3.run(LocationServiceImpl.java:366)
       at org.chromium.content.browser.ThreadUtils.runOnUiThreadBlocking(ThreadUtils.java:27)
       at com.marianhello.bgloc.service.LocationServiceImpl.start(LocationServiceImpl.java:363)
       at com.marianhello.bgloc.service.LocationServiceImpl.onStartCommand(LocationServiceImpl.java:274)
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4776)
       at android.app.ActivityThread.access$2000(ActivityThread.java:301)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2196)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:246)
       at android.app.ActivityThread.main(ActivityThread.java:8595)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Expected Behavior

Exception can be handled properly

Actual Behavior

App crashes with error

Fatal Exception: java.lang.RuntimeException
Unable to start service com.marianhello.bgloc.service.LocationServiceImpl@ee59593 with null: java.lang.IllegalArgumentException: invalid null provider

Possible Fix

Error happens in https://github.com/mauron85/background-geolocation-android/blob/58464febf6901f1e489817e4425418d48443929b/src/main/java/com/marianhello/bgloc/provider/RawLocationProvider.java#L49 because system somehow can't find suitable location provider and locationManager.getBestProvider(criteria, true) returns null:

try {
    locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), mConfig.getInterval(), mConfig.getDistanceFilter(), this);
    isStarted = true;
} catch (SecurityException e) {
    logger.error("Security exception: {}", e.getMessage());
    this.handleSecurityException(e);
}

The try {} catch {} block only handles SecurityException but should also handle any exception . Also, there should be a check of getBestProvider function call result.

Steps to Reproduce

1. 2. 3. 4.

Context

Debug logs

nassimmiled commented 1 year ago

It's the same issue that happened to me. when I remove the location access from my app