Search code examples
javaandroidandroid-studioandroid-permissionsmapfragment

Android Studio @androidx.annotation.NonNull error when onRequestPermissionResult is called inside MapFragment


Hello I am still fairly new to this, so please help me understand why I am having this error. I have tried many solutions, so I'm just going to list everything I've done since I can't seem to understand why this is happening.

I created a project that integrates GoogleMaps at min SDK 21 to target/compile at SDK 28. I did call the permissions needed inside the Manifesto.

I created a file that extends the MapFragment class and everything seems to be working fine. I am able to check and request permission for the user's location (the box does show up), but when I called the onRequestPermissionResult method it is shown differently and gives me an error saying "error: cannot find symbol class NonNull":

@Override
public void onRequestPermissionsResult(int requestCode, @androidx.annotation.NonNull String[] permissions, @androidx.annotation.NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
} 

In my other Fragment (android.support.v4.app) classes the @androidx.annotation.NonNull is @NonNull instead. I first thought maybe I needed to add implementation 'com.android.support:support-annotations:28.0.0' to the build.gradle, but that wasn't the case. I then tried to just replace the @androidx.annotation.NonNull with @NonNull which made the error go away, but whenever I clicked allow or deny it wasn't hitting the onRequestPermissionResult method.

I created a method that checks for a permission, but it won't let me use requestPermission on its own without checking if the build is greater or equal to SDK 23, but my min SDK is 21. So instead I just checked if the build is greater or equal to 21 and used ActivityCompat to get the requestPermission method and it works. It will check and ask for permission, so I'm thinking maybe the onRequestPermissionResult only works in the MainActivity which is what I don't want. I want to be able to call a method after checking if the request was granted inside the MapFragment. Is it because MapFragment isn't supported with android.support.v4.app? It looks like this:

 private boolean checkAskPermission(int requestCode, String permissionString){
    if(Build.VERSION.SDK_INT >= 21){
        int permission = ContextCompat.checkSelfPermission(mContext, permissionString);
        if(permission!=PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(getActivity(), new String[]{permissionString}, requestCode);
            return false;
        }
    }
    return true;
}

At this point I don't know what else to try. I thought maybe I wasn't checking the permission correctly inside onRequestPermissionResult when I change @androidx.annotation.NonNull to @NonNull to be able to use it, but the method doesn't hit when I use a break on it.

Please leave detail responses, so I can fully understand my problem. I have been stuck on this for a day.

Edit: Solution

 private boolean checkAskPermission(int requestCode, String permissionString){
    if(Build.VERSION.SDK_INT >= 23){
        int permission = ContextCompat.checkSelfPermission(mContext, permissionString);
        if(permission!=PackageManager.PERMISSION_GRANTED){
            requestPermissions(new String[]{permissionString}, requestCode);
            return false;
        }
    }
    return true;
}

and just changed the @androidx.annotation.NonNull to @NonNull and now it hits the method.

Thanks to Eugene for clearing up SDK permissions. Only SDK 23 and higher require permission.


Solution

  • [...] but the method doesn't hit when I use a break on it.

    [...] used ActivityCompat to get the requestPermission [...]

    You're in a fragment. Don't ask the activity for permissions. If you ask the fragment you also get callback in the fragment.

    requestPermissions(new String[]{permissionString}, requestCode);
    

    Runtime permissions were introduced in API 23 (that's why the method is only available since API 23). Fix your condition. Before that having the permission declared in manifest is enough.

    Off-topic: Platform fragments have been deprecated. Use support fragments instead. Extend SupportMapFragment.

    @androidx.annotation.NonNull cannot be found because you don't use AndroidX in your project. You can use whatever other @NonNull annotation is available (either from the SDK or from the support library).