Issue
I can't seem to figure out, why is this causing an exception.
GPSListener gps = new GPSListener(); // <- implements ILocationListener interface
(LocationManager)GetSystemService(LocationService).RequestLocationUpdates(LocationManager.GpsProvider, 0, 0, gps); // <- System.NullReferenceException
Calling IsProviderEnabled
for the "gps" provider returns true
.
GetSystemService
does also return a valid LocationManager and not a null. gps
object is also not null. ACCESS_COARSE_LOCATION
and ACCESS_FINE_LOCATION
are also added to the manifest.
The call stack
at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V (_JniMarshal_PP_V callback, System.IntPtr jnienv, System.IntPtr klazz) [0x0001c] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23
at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(intptr,intptr)
The internal exception message is "Object reference not set to an instance of an object."
How can i find out more information what is actually not set in this situation. And how could i fix it. The error happens both in emulator (API 30) and physical phone with Android 7.
Solution
Please make sure your app has been granted the permission about the location by the user, not only declare it in the AndroidManifest.xml. And you can add the following code into the oncreate method of the activity to do that.
var status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
In addition, if you call the method in the background service, you need to add the ACCESS_BACKGROUND_LOCATION
in the AndroidManifest.xaml and use the following code:
var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
Answered By - Liyun Zhang - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.