CrossGeeks / GeofencePlugin

Geofence Plugin for Xamarin iOS and Android
MIT License
48 stars 22 forks source link

Crash | CrossGeofence.Current.StartMonitoring crash in Android after clean application data #31

Open RumbosN opened 4 years ago

RumbosN commented 4 years ago

When I clean the application data (including cache) and run CrossGeofence.Current.StartMonitoring. The plugin crash and the exception is: Message: System.NullReferenceException: 'Object reference not set to an instance of an object.' StackTrace:

at Plugin.Geofence.GeofenceImplementation.RequestMonitoringStart () [0x00000] in <793ebe419a94494388f459127f8240ff>:0 
at Plugin.Geofence.GeofenceImplementation.StartMonitoring (Plugin.Geofence.GeofenceCircularRegion region) [0x00061] in <793ebe419a94494388f459127f8240ff>:0 

When I read the GeofencePlugin code, I think it's because mGoogleApiClient isn't initialized.

Andorid Service:

    using Android.App;

    [Service]
    public class GeofenceService : Service
    {
        public override void OnCreate()
        {
            base.OnCreate();

            System.Diagnostics.Debug.WriteLine("Geofence Service - Created");
        }

        public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
        {
            System.Diagnostics.Debug.WriteLine("Geofence Service - Started");
            return StartCommandResult.Sticky;
        }

        public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
        {
            System.Diagnostics.Debug.WriteLine("Geofence Service - Binded");
            return null;
        }

        public override void OnDestroy()
        {
            System.Diagnostics.Debug.WriteLine("Geofence Service - Destroyed");
            base.OnDestroy();
        }
    }

In MainApplication

public class MainApplication : Application, Application.IActivityLifecycleCallbacks
    {
        public static Context AppContext;
        public MainApplication(IntPtr javaReference, JniHandleOwnership transer)
          :base(javaReference, transer)
        {
        }
        public override void OnCreate()
        {
            base.OnCreate();

            AppContext = this.ApplicationContext;

            RegisterActivityLifecycleCallbacks(this);

            //TODO: Initialize CrossPushNotification Plugin
            //TODO: Replace string parameter with your Android SENDER ID
            //TODO: Specify the listener class implementing IPushNotificationListener interface in the Initialize generic
            //CrossPushNotification.Initialize<CrossPushNotificationListener>("<ANDROID SENDER ID>");
            CrossGeofence.Initialize<CrossGeofenceListener>();
            CrossGeofence.RequestLocationPermission = true;
            //Start a sticky service to keep receiving geofence events when app is closed.
            StartService();
        }

        public static void StartService()
        {
            AppContext.StartService(new Intent(AppContext, typeof(GeofenceService)));

            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
            {

                PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(GeofenceService)), 0);
                AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
                alarm.Cancel(pintent);
            }
        }

        public static void StopService()
        {
            AppContext.StopService(new Intent(AppContext, typeof(GeofenceService)));
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
            {
                PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(GeofenceService)), 0);
                AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
                alarm.Cancel(pintent);
            }
        }
bear1770 commented 4 years ago

Does anyone have any ideas on this one? I'm getting the same exact error.

clintonrocksmith commented 4 years ago

We also have this issue, just wondering if you're using AndroidX? I'm wondering if the solution needs and upgrade.

For us, it causes an ANR on certain devices.

clintonrocksmith commented 4 years ago

Hi all,

For anyone who has this, we added the plugin namespace to the linker to exclude it. Then it started working no problems.

My assumption is that the AndroidX migration legacy helper was stripping something out.

Does this work for you?

clintonrocksmith commented 3 years ago

FYI, we found the crash was fixed when we downgraded to 1.5.4 of the plugin.