jamesmontemagno / GeolocatorPlugin

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

Error getting position on Android #298

Open Giulio64 opened 5 years ago

Giulio64 commented 5 years ago

For common questions and issues see the FAQ

Bug Information

Invoking the method GetPositionAsync() makes the app crash only on android with

Version Number of Plugin: 4.5.0 Device Tested On:Samsung S8 Simulator Tested On: - Version of VS: 8.0.2 Mac Version of Xamarin: 3.1.0 Versions of other things you are using: Plugin.Permission 3.0.0.12

Steps to reproduce the Behavior

Install Plugin.Permission latest version install Xam.Plugin.Geolocator

request position by locator.GetPositionAsync();

Expected Behavior

Actual Behavior

crash with : Fatal signal 11 (SIGSEGV), code 2, fault addr 0x7ff9c4dfe0 in tid 14440

Code snippet

private static async Task ottieniPosizione(List scaricati) { if(!await autorizzaGeolocalizzazione()){ return; }

        Position position = null;

        try
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100;

            if (! locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
            {
                return;
            }

            position = await locator.GetPositionAsync();

        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            Debug.WriteLine(ex.StackTrace);
            Debug.WriteLine(ex.Source);

            return;
        }

        if (position == null){
            return;
        }

        DistanzaPosizione(scaricati, position);

    }

//Main activity

using System;

using Android.App; using Android.Content; using Android.Content.PM; using Android.OS; using Plugin.FirebasePushNotification; using Firebase; using Plugin.CurrentActivity; using Plugin.Permissions; using SegmentedControl.FormsPlugin.Android; using ZXing.Net.Mobile; using CarouselView.FormsPlugin.Android; using ZXing.Mobile;

namespace CTSApp.Droid { [Activity(Label = "CTSApp", Icon = "@drawable/ic_launcher", Theme = "@style/MyTheme.Splash", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

{
    protected override void OnCreate(Bundle bundle)
    {

        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);

        CarouselViewRenderer.Init();
        MobileBarcodeScanner.Initialize(Application);
        Rg.Plugins.Popup.Popup.Init(this, bundle);
        ZXing.Net.Mobile.Forms.Android.Platform.Init();
        FFImageLoading.Forms.Droid.CachedImageRenderer.Init(enableFastRenderer: true);
        Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
        CrossCurrentActivity.Current.Init(this, bundle);
        SegmentedControlRenderer.Init();
        FirebasePushNotificationManager.ProcessIntent(Intent);

        LoadApplication(new App());

    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult (requestCode, permissions, grantResults);           

    }
}

}

Screenshotst

rohitvipin commented 5 years ago

Why are you calling multiple times permission request? You need to have only one.

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults); }