Ks.P
Ks.P

Reputation: 222

Xamarin.Essentials "The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class."

My application gives an error:

The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class.

I use the emulator Genymotion. GPS enabled

public async Task btnAdd_Click()
    {
        try
        {
            var request = new GeolocationRequest(GeolocationAccuracy.Medium);
            var location = await Geolocation.GetLocationAsync(request);

            if (location != null)
            {
                string note = "GPS: Latitude-" + location.Latitude + " Longitude-" + location.Longitude;
            }
        }
        catch (Exception ex)
        {
            // Unable to get location
        }
    }

Upvotes: 15

Views: 11737

Answers (4)

SLdragon
SLdragon

Reputation: 1627

First you need init Xamarin Essentials in OnCreate function from MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    ...

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);

    ...
}

Then overide the function should work

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Upvotes: 2

Everton Costa
Everton Costa

Reputation: 1

you can use

using Plugin.Permissions;
using Plugin.CurrentActivity;

and in the MainActivity.cs file, OnCreate Method, insert:

CrossCurrentActivity.Current.Init(this, savedInstanceState);

Upvotes: 0

HO LI Pin
HO LI Pin

Reputation: 1681

Improve Gerald answer by offer full code

    protected async override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        txtnumber = FindViewById<TextView>(Resource.Id.textView1);
        txtnumber.Text ="Initializing";



        try
        {

            var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
            var location = await Geolocation.GetLocationAsync(request);
            txtnumber.Text = "finish read geolocation";


            if (location != null)
            {
                txtnumber.Text = "GPS: Latitude-" + location.Latitude + " Longitude-" + location.Longitude;
            }
        }
        catch (FeatureNotSupportedException fnsEx)
        {
            // Handle not supported on device exception
        }
        catch (FeatureNotEnabledException fneEx)
        {
            // Handle not enabled on device exception
        }
        catch (PermissionException pEx)
        {
            // Handle permission exception
        }
        catch (Exception ex)
        {
            // Unable to get location
            txtnumber.Text = ex.Message.ToString();
        }

    }

Upvotes: 4

Gerald Versluis
Gerald Versluis

Reputation: 34003

Like the error states, did you add this line:

Xamarin.Essentials.Platform.Init(this, bundle);

In your MainActivity.cs in the OnCreate method?

Upvotes: 33

Related Questions