Xamarin.Essentials"无法检测到当前的活动.确保在Activity或Application类中调用了Init."

Kse*_*aya 5 c# xamarin.forms

我的应用程序出错了:

无法检测到当前的活动.确保在Activity或Application类中调用了Init.

我使用模拟器Genymotion.启用GPS

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
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ger*_*uis 12

与错误状态一样,您是否添加了此行:

Xamarin.Essentials.Platform.Init(this, bundle);
Run Code Online (Sandbox Code Playgroud)

在你MainActivity.csOnCreate方法?