entLocationという名前のエントリ ボックスが focus されたときに、デバイスの経度と緯度を取得したいと考えています。Xamarin.Essential Geolocationを使用してデバイスの GPS 位置を取得していますが、ドキュメントとチュートリアルに従っても GPS 位置を取得できません。Androidマニフェストに以下の許可を追加しましたが、まだ何もありません。"Error3"で表示アラートが表示されます。Error3はUnable to get locationの例外です。
エラーは「Java.Lang.NullPointerException: null オブジェクト参照で仮想メソッド 'void android.app.Activity.requestPermissions(java.lang.String[],int)' を呼び出そうとしています」
私の質問:
1. 私のコードのエラーは何ですか?
2. GPS 位置情報をオフラインにすることはできますか? はいの場合、どうすればそれを達成できますか?
use-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-feature android:name="android.hardware.location" android:required="false"
を使用-feature android:name="android.hardware.location.gps" android:required="false"
uses-feature android:name="android.hardware.location.network" android:required="false"
private async void entLocation_FocusedAsync(object sender, FocusEventArgs e)
{
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
if (location != null)
{
entLocation.Text = location.Longitude.ToString() + "," + location.Latitude.ToString();
}
}
catch (FeatureNotSupportedException fnsEx)
{
await DisplayAlert("Error1", fnsEx.ToString(), "ok");
}
catch (PermissionException pEx)
{
await DisplayAlert("Error2", pEx.ToString(), "ok");
}
catch (Exception ex)
{
await DisplayAlert("Error3", ex.ToString(), "ok");
}
}