jamesmontemagno / GeolocatorPlugin

Geolocation plugin for Xamarin and Windows
MIT License
292 stars 158 forks source link

Plugin.Geolocator.GeolocationContinuousListener.OnLocationChanged - Object reference not set to an instance of an object #306

Open igorgomeslima opened 5 years ago

igorgomeslima commented 5 years ago

Version Number of Plugin: \packages\Xam.Plugin.Geolocator.4.5.0.6\lib\monoandroid71\Plugin.Geolocator.dll Device Tested On: Android Version of VS: VS2017 15.9.13 Version of Xamarin: 4.12.3.83 Xamarin Android SDK: 12.4.0.64 Versions of other things you are using: Mock Locations (fake GPS path)(https://play.google.com/store/apps/details?id=ru.gavrikov.mocklocations&hl=pt_BR)

Steps to reproduce the Behavior

Default configuration: https://jamesmontemagno.github.io/GeolocatorPlugin/

Expected Behavior

Position Changed event called.

Actual Behavior

This happens after a certain amount of time (this is intermittent).

Thread started: #44 Thread started: #45 Thread started: #46 Thread started: #47 07-05 14:37:21.686 D/Mono (12018): DllImport attempting to load: '/system/lib/liblog.so'. 07-05 14:37:21.693 D/Mono (12018): DllImport loaded library '/system/lib/liblog.so'. 07-05 14:37:21.693 D/Mono (12018): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so'). 07-05 14:37:21.693 D/Mono (12018): Searching for 'android_log_print'. 07-05 14:37:21.694 D/Mono (12018): Probing 'android_log_print'. 07-05 14:37:21.694 D/Mono (12018): Found as 'android_log_print'. Thread started: #48 Thread started: #49 Thread started: #50 Thread started: #51 Thread started: #52 Thread started: #53 Thread started: #54 07-05 14:37:21.791 I/MonoDroid(12018): UNHANDLED EXCEPTION: 07-05 14:37:21.806 I/MonoDroid(12018): System.NullReferenceException: Object reference not set to an instance of an object. 07-05 14:37:21.806 I/MonoDroid(12018): at **_Plugin.Geolocator.GeolocationContinuousListener.OnLocationChanged (Android.Locations.Location location) [0x0007e] in :0 07-05 14:37:21.806 I/MonoDroid(12018): at Android.Locations.ILocationListenerInvoker.n_OnLocationChanged_Landroid_locationLocation (System.IntPtr jnienv, System.IntPtr nativethis, System.IntPtr nativelocation) [0x00011] in **:0 07-05 14:37:21.806 I/MonoDroid(12018): at (wrapper dynamic-method) System.Object.27(intptr,intptr,intptr) Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

Thread finished: #54 The thread 0x36 has exited with code 0 (0x0). Thread finished: #52 The thread 0x34 has exited with code 0 (0x0). Thread finished: #51 The thread 0x33 has exited with code 0 (0x0). Thread finished: #47 The thread 0x2f has exited with code 0 (0x0). Thread finished: #46 The thread 0x2e has exited with code 0 (0x0). Thread finished: #48 The thread 0x30 has exited with code 0 (0x0). Thread finished: #50 The thread 0x32 has exited with code 0 (0x0). Thread finished: #53 The thread 0x35 has exited with code 0 (0x0). Thread finished: #44 The thread 0x2c has exited with code 0 (0x0). Thread finished: #45 The thread 0x2d has exited with code 0 (0x0). 07-05 14:38:43.014 E/mono-rt (12018): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object. 07-05 14:38:43.014 E/mono-rt (12018): at (wrapper dynamic-method) System.Object.27(intptr,intptr,intptr) 07-05 14:38:43.014 E/mono-rt (12018): at (wrapper native-to-managed) System.Object.27(intptr,intptr,intptr) Thread started: #55 Thread started: #56 Debugger Connection Lost: Debugger lost connection to the running application. Likely this means the application terminated unexpectedly.

Code snippet

async Task StartListeningPositionChangedAsync()
{
    try
    {
        if (CrossGeolocator.Current.IsListening)
            return;

        await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(2),10, true);

        CrossGeolocator.Current.PositionChanged += PositionChanged;
        CrossGeolocator.Current.PositionError += PositionError;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void PositionChanged(object sender, PositionEventArgs e)
{
    try
    {
        if (e.Position != null)
        {
            ...
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
harry-gordon commented 5 years ago

I'm also experiencing this on Android, so far only on the Samsung S9. For context we listen for location updates throughout most of the app but sometimes we experience this same crash:

GeolocationContinuousListener.OnLocationChanged (Android.Locations.Location location)
ILocationListenerInvoker.n_OnLocationChanged_Landroid_location_Location_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_location)

@igorgomeslima did you ever find a work-around for this? It's a really serious issue for us on Android.

igorgomeslima commented 5 years ago

@harry-gordon We not find any work-around about this. This really is very serious. In this case, we stopped using the plugin. Now we using Fused Location Provider.

Sample here.

chrisfoulds commented 5 years ago

I've just done a 5hr soak test using lockito as my fake gps provider and have encountered zero issues, yesterday I ran the app live for 2hr 45mins with no issues either.

Code is :

 void CrossGeolocator_Current_PositionChanged(object sender, PositionEventArgs e)
        {
            var position = e.Position;
            if (lastRecievedPosition != null)
            {
                if(e.Position.Latitude==lastRecievedPosition.Latitude && e.Position.Longitude == lastRecievedPosition.Longitude && e.Position.Timestamp == lastRecievedPosition.Timestamp)
                {
                    // Reject it we got a double fire !!!
                    return;
                }
                else
                {
                    lastRecievedPosition = new StorePosition(e.Position);
                }
            }
            else
            {
                lastRecievedPosition = new StorePosition(e.Position);
            }
..... etc. etc.
}
chrisfoulds commented 5 years ago

and


if (CrossGeolocator.Current.IsListening)
            {
                return;
            }

            lastRecievedPosition = null;
            lastSentPosition = null;
            CrossGeolocator.Current.PositionChanged += CrossGeolocator_Current_PositionChanged;
            CrossGeolocator.Current.PositionError += CrossGeolocator_Current_PositionError;

            // was 1,1
            if (await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 1,
                true, new ListenerSettings
                {
                    ActivityType = ActivityType.Fitness,
                    AllowBackgroundUpdates = true,
                    DeferLocationUpdates = false,
                    DeferralDistanceMeters = 500,
                    DeferralTime = TimeSpan.FromSeconds(60 * 5),
                    ListenForSignificantChanges = false,
                    PauseLocationUpdatesAutomatically = false,
                }))
            {
                tracking = true;
            }
            else
            {
                tracking = false;
            }
harry-gordon commented 5 years ago

Hey @chrisfoulds, thanks for sharing. To put the issue in perspective we've been using the plugin for weeks and have only encountered the issue on one device and only with mock data.

Will continue to gather data and consider submitting a fix 🕵.