ilovecoding
ilovecoding

Reputation: 11

Unable to resume activity when getting users response for permission

I'm trying to get user's permission to access their GPS. I'm not sure whether I have used it correctly because when i visit to that screen I get a crash with the error:

java.lang.RuntimeException: Unable to resume activity {com.SuppShoesTest.SuppShoesTest/md5056fe371b3902b2cc984ad5dda4f920b.gMaps}: android.util.SuperNotCalledException: Activity {com.SuppShoesTest.SuppShoesTest/md5056fe371b3902b2cc984ad5dda4f920b.gMaps} did not call through to super.onResume() 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.app.ActivityThread.-wrap11(ActivityThread.java) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.os.Handler.dispatchMessage(Handler.java:102) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.os.Looper.loop(Looper.java:148) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.app.ActivityThread.main(ActivityThread.java:5417) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at java.lang.reflect.Method.invoke(Native Method) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 01-18 23:57:20.419 E/AndroidRuntime( 2712): Caused by: android.util.SuperNotCalledException: Activity {com.SuppShoesTest.SuppShoesTest/md5056fe371b3902b2cc984ad5dda4f920b.gMaps} did not call through to super.onResume() 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.app.Activity.performResume(Activity.java:6314) 01-18 23:57:20.419 E/AndroidRuntime( 2712): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)

Here is my code

{
    private GoogleMap maps;
    string dman;
    string date;
    string time;
    public static readonly EndpointAddress EndPoint = new EndpointAddress("http://192.168.1.3:9608/SuppShoes.svc");
    private SuppShoesClient _client;
    ArrayList postal;
    double mylat;
    double mylong;
    Location _currentLocation;
    LocationManager _locationManager;
    LinearLayout layout;
    string _locationProvider;
    PolylineOptions rect = new PolylineOptions();
    readonly string[] PermissionsLocation = 
{
  Manifest.Permission.AccessCoarseLocation,
  Manifest.Permission.AccessFineLocation
};

    const int RequestLocationId = 0;
    protected override void OnCreate(Bundle savedInstanceState)
    {

        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.gMap);
        layout = (LinearLayout)FindViewById(Resource.Id.layout);
        InitializeHelloWorldServiceClient();
        dman = Intent.GetStringExtra("Dman");
        date = Intent.GetStringExtra("date");
        time = Intent.GetStringExtra("Time");
        InitializeLocationManager(); 
        SetUpMap();


        // Create your application here
    }

    private void InitializeHelloWorldServiceClient()
    {


        BasicHttpBinding binding = CreateBasicHttp();
        _client = new SuppShoesClient(binding, EndPoint);


    }

    private static BasicHttpBinding CreateBasicHttp()
    {
        BasicHttpBinding binding = new BasicHttpBinding
        {
            Name = "basicHttpBinding",
            MaxBufferSize = 2147483647,
            MaxReceivedMessageSize = 2147483647
        };
        TimeSpan timeout = new TimeSpan(0, 0, 30);
        binding.SendTimeout = timeout;
        binding.OpenTimeout = timeout;
        binding.ReceiveTimeout = timeout;
        return binding;
    }

    public async void OnLocationChanged(Location location)
    {
        _currentLocation = location;
        if (_currentLocation == null)
        {

        }
        else
        {

            AddMarker();
        }

    }
    public void OnProviderDisabled(string provider) { }

    public void OnProviderEnabled(string provider) { }

    public void OnStatusChanged(string provider, Availability status, Bundle extras) { }




    protected override void OnResume()
    {


            const string permission = Manifest.Permission.AccessFineLocation;
            if (CheckSelfPermission(permission) == (int)Permission.Granted)
            {
                base.OnResume();
                _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);

            }
        if (ShouldShowRequestPermissionRationale(permission))
              {

                //Explain to the user why we need to read the contacts
                  Android.Support.Design.Widget.Snackbar.Make(layout,"Location access is required to show coffee shops nearby.", Android.Support.Design.Widget.Snackbar.LengthIndefinite)
                        .SetAction("OK", v => RequestPermissions(PermissionsLocation, RequestLocationId))
                        .Show();
                return;
              }

//Finally request permissions with the list of permissions and Id RequestPermissions(PermissionsLocation, RequestLocationId); }

                    public override async void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
                    {
                      switch (requestCode)
                      {
                        case RequestLocationId:
                        {
                          if (grantResults[0] == Permission.Granted)
                          {
                            //Permission granted
                              var snack = Android.Support.Design.Widget.Snackbar.Make(layout, "Location permission is available, getting lat/long.", Android.Support.Design.Widget.Snackbar.LengthShort);
                            snack.Show();

                            base.OnResume();
                          }
                          else
                          {
                            //Permission Denied :(
                            //Disabling location functionality
                              var snack = Android.Support.Design.Widget.Snackbar.Make(layout, "Location permission is denied.",  Android.Support.Design.Widget.Snackbar.LengthShort);
                            snack.Show();
                          }
                        }
                        break;
                      }
                    }
    protected override void OnPause()
    {
        base.OnPause();
        _locationManager.RemoveUpdates(this);
    }
    void InitializeLocationManager()
    {
        _locationManager = (LocationManager)GetSystemService(LocationService);
        Criteria criteriaForLocationService = new Criteria
        {
            Accuracy = Accuracy.Fine
        };
        IList<string> acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);

        if (acceptableLocationProviders.Any())
        {
            _locationProvider = acceptableLocationProviders.First();
        }
        else
        {
            _locationProvider = string.Empty;
        }

    }

    void AddMarker()
    {
        RunOnUiThread(() =>
        {
            LatLng current = new LatLng(_currentLocation.Latitude, _currentLocation.Longitude);
            MarkerOptions asd = new MarkerOptions();

            asd.SetPosition(current);

            asd.InvokeIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.green));
            maps.AddMarker(asd);
            rect.Add(current);
        });
    }
    private void SetUpMap()
    {
        if(maps==null)
        {
            FragmentManager.FindFragmentById<MapFragment>(Resource.Id.googlemap).GetMapAsync(this);
        }

    }
    public void OnMapReady(GoogleMap googleMap)
    {
        this.maps = googleMap;

        LatLng latlng = new LatLng(Convert.ToDouble(1.3521), Convert.ToDouble(103.8198));
        CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng,10);
        maps.MoveCamera(camera);

        _client.getPostalAsync(dman, date, time);
        _client.getPostalCompleted += _client_getPostalCompleted;
    }


    private void _client_getPostalCompleted(object sender, getPostalCompletedEventArgs e)
    {

        RunOnUiThread(() =>
        {


        foreach (var item in e.Result)
        {


            LatLng latlng = new LatLng(item.lat1, item.longi2);
            MarkerOptions options = new MarkerOptions()    
            .SetPosition(latlng)
            .SetTitle(item.companyname2)
            .SetSnippet(item.DevID.ToString());
            maps.AddMarker(options);
            rect.Add(latlng);
            Polyline polyline = maps.AddPolyline(rect);
        }
    });


    }
}

Upvotes: 1

Views: 1163

Answers (2)

Naresh Mittal
Naresh Mittal

Reputation: 261

For Xamarin forms, base.OnResume() need to be called to avoid this exception. As always, this need to be at the top of overridden OnResume() method.

Upvotes: 2

W4R10CK
W4R10CK

Reputation: 5550

As clear by your Exception Caused by: android.util.SuperNotCalledException, you must add super.onResume() to perform resume life cycle because your are @Overriding the method.

Call super inside onResume();:

super.onResume();

Upvotes: 0

Related Questions