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

11,636

Solution 1

[...] 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).

Solution 2

import that annotation class:

import androidx.annotation.NonNull;

which is either coming from this dependency:

implementation "androidx.annotation:annotation:1.0.0"

or from this dependency, in case not yet using androidx:

implementation "com.android.support:support-annotations:28.0.0"

then you can use it as usual:

 @NonNull

Solution 3

The only work-around that I found useful was to fore-go the react-native run-android command and manually go into the react-native library (for me: ProjectHome/node_modules/ModuleIWantToChange/android/src/main/java/FileToChange.java) that was importing androidx.annotation.NonNull, changing that dependency to android.support.annotation.NonNull and then making sure to compile with the com.android.support:support-annotations under "dependencies" in that node_module's "android/build.gradle" file. In my case, I'm using version "com.android.support:support-annotations:25.3.1". You'll have to make sure that you have the version you call out here. Look at what you have installed with Android Studio under $ANDROID_HOME/extras/android/m2repository/com/android/support/support-annotations.

Then, instead of react-native run-android, I moved to the android directory under the project's home directory and ran sudo ./gradlew --stacktrace installDebug (stacktrace is optional) to build and install the apk on my emulator/device.

This might not work for everyone, but it was a tolerable fix in my case.

Share:
11,636

Related videos on Youtube

Pandda
Author by

Pandda

Updated on June 04, 2022

Comments

  • Pandda
    Pandda about 2 years

    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.

  • Pandda
    Pandda over 5 years
    So the user doesn't need to be asked for permission to read and write to external storage if their SDK is lower than 23? I just need to check for 23 and up
  • Eugen Pechanec
    Eugen Pechanec over 5 years
    Yeah, why did you think it was 21?
  • Pandda
    Pandda over 5 years
    I just had to change the condition and everything works now. I think I just got it confused with "after 23 permissions were introduced" as in always having to request no matter what, but now I understand it means anything SDK 23 and up. Thanks!! I also changed MapFragment to SupportMapFragment