Can anyone tell me why with Android I get a null reference exception calling this on app load in OnAppearing but in IOS I do not get a null ref exception?
I get the null ref on Android if permissions have not yet been granted and I have to ask for permissions.
public async Task PermissionCheck(Permission permission)
{
PermissionStatus status = PermissionStatus.Unknown;
switch (permission) { case Permission.Location: status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>(); break; case Permission.Storage: status = await CrossPermissions.Current.CheckPermissionStatusAsync<StoragePermission>(); break; } if (status != PermissionStatus.Granted) { switch (permission) { case Permission.Location: status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>(); break; case Permission.Storage: status = await CrossPermissions.Current.RequestPermissionAsync<StoragePermission>(); break; } } return status;
Answers
I tried to use the sample of PermissionsPlugin to test, but I couldn't reproduce the null reference exception as you mentioned.
Could you please share your detailed steps and screenshots with me?
Xamarin forums are migrating to a new home on Microsoft Q&A!
We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
For more information, please refer to this sticky post.
@jezh My app is a Shell Flyout app. When the app first loads, in the main tab page "OnAppearing" is where if I call
status = await CrossPermissions.Current.RequestPermissionAsync();
I will get the null ref. OTHERWISE it works perfect.....when I call it AFTER the app has finished loading
I did a test with a Shell Flyout app, but I couldn't reproduce this problem. Could you please post a basic demo to github or onedriver so that we can test on our side?
I already posted on gethub.
The main tab in the flyout app, that loads when the app starts, i have the following
protected async override void OnAppearing()
{
// this will cause Android to fail
status = await CrossPermissions.Current.RequestPermissionAsync();
// do some more work
base.OnAppearing();
}
And this ONLY happens when the app starts.
Microsoft Visual Studio Professional 2019 Version 16.8.4
Xamarin Essentials 1.6.1
Xamarin Forms 5.0.0.1931
*NOTE I should state....that it ONLY fails if you do NOT have permissions.
Could you please post the github link so that we can check and test it on our side?
https://github.com/xamarin/Xamarin.Forms/issues/13598
There seems to be a similar one also
https://github.com/xamarin/Xamarin.Forms/issues/13611
Crap that was the wrong thread again! I have a few issues going on, that is for navigation not working. https://github.com/jamesmontemagno/PermissionsPlugin/issues/190
So I got this to work on the newer android by put a Task.delay(1000) after the call.
However.....on the Android 7 phone, api 24, my call to check of location services is on, returns false....even though Location Services is turned on.
Congrats, you took a step forward.
Would you mind sharing your project or a sample project reproducing the issue?
I cannot share my project, it has proprietary information in it. What I am noticing is I have a class I initialize to kick off the PLugin BLE monitoring. I have the creation of the class in OnCreate of App.xaml. The instantiation of this class seems to happen at different times whether its andorid, ios or a particular android device. I notice this because there are a series of events I need to happen in a particular order.
@jezh Do you have any guidance of how to monitor BLE connections when the device goes to sleep....the device being IOS or Android.....