GeofencePlugin icon indicating copy to clipboard operation
GeofencePlugin copied to clipboard

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

Open RumbosN opened this issue 5 years ago • 4 comments

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);
            }
        }

RumbosN avatar Apr 27 '20 16:04 RumbosN

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

bear1770 avatar Jul 16 '20 18:07 bear1770

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 avatar Jul 25 '20 21:07 clintonrocksmith

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 avatar Jul 27 '20 04:07 clintonrocksmith

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

clintonrocksmith avatar Jul 30 '20 23:07 clintonrocksmith