Forum Libraries, Components, and Plugins

Plugin.Permissions.Abstractions null ref with droid but not ios

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

  • jezhjezh Member, Xamarin Team Xamurai

    Can anyone tell me why with Android I get a null reference exception calling this on app load in OnAppearing

    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.

  • sisaackssisaacks Member ✭✭✭✭

    @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

  • jezhjezh Member, Xamarin Team Xamurai
    edited February 3

    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?

  • sisaackssisaacks Member ✭✭✭✭

    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();
    }

  • sisaackssisaacks Member ✭✭✭✭

    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

  • sisaackssisaacks Member ✭✭✭✭

    *NOTE I should state....that it ONLY fails if you do NOT have permissions.

  • jezhjezh Member, Xamarin Team Xamurai

    I already posted on gethub.

    Could you please post the github link so that we can check and test it on our side?

  • sisaackssisaacks Member ✭✭✭✭

    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

  • sisaackssisaacks Member ✭✭✭✭

    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.

  • jezhjezh Member, Xamarin Team Xamurai

    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?

  • sisaackssisaacks Member ✭✭✭✭

    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.....

Sign In or Register to comment.