Hi, I'm seeing below fatal exception (application is crashing) when I request the location permission on Android emulator. It works fine on iOS.
008-14 22:37:42.151 5042 5042 E AndroidRuntime: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
08-14 22:37:42.151 5042 5042 E AndroidRuntime: at com.facebook.react.modules.permissions.PermissionsModule$1.invoke(PermissionsModule.java:119)
08-14 22:37:42.151 5042 5042 E AndroidRuntime: at com.facebook.react.modules.permissions.PermissionsModule.onRequestPermissionsResult(PermissionsModule.java:207)
Could you please let me know why this might be happening?
Very interesting. This package does not have native code for Android, so you've likely caught a bug in react-native itself.
This line is crashing because the results array is empty.
A related stackoverflow answer explains that, if the permission request is cancelled, the array will be empty. Is that how you reproduce the bug? If so, I'd encourage to submit this to react-native repo as well.
RN should have checked results.length > 0 before accessing the array.
I actually don't cancel the request at all. I just simply use:
Permissions.request('location', 'whenInUse')
.then(response => {
console.log(response);
this.setState({
locationPermissionsGranted: response === 'authorized' ? true : false
})
})
and the app crashes. I have tried without the "whenInUse" part as well, thinking maybe that was just for iOS. I also did add:
<uses-feature android:name="android.hardware.location.gps"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
to my AndroidManifest.xml
Anything in the above stand out as wrong? Otherwise I'll submit a bug for ReactNative. I'm just worried they'll point to your lib as the source of error!
Interestingly, when I re-visited this issue today, it was gone after uninstalling the app and running react-native run-android again! Sorry for the bother, guys.
I just encountered this issue. Is there any known solution?
@SamMatthewsIsACommonName To me, it just happened and it was because I forgot to add the permission request in the AndroidManifest. As soon as I tried to request the permission, the app crashed. Now I have another issue and is that the permission comes back as true even if I disable the permission. I don't think is related tho.
I met it today.
out of index because of the following code.
results[0] == PackageManager.PERMISSION_GRANTED
I was having this issue until I set the targetSdk as 26 in my build.gradle and also added the fine location permission in the android manifest. Haven't had this problem again.
After I dig it deeper, I think what makes this exception because of the method of
void onRequestPermissionsResult (int requestCode,
String[] permissions,
int[] grantResults)
The doc declare that
grantResults | int: The grant results for the corresponding permissions which is either聽PERMISSION_GRANTED聽or聽PERMISSION_DENIED. Never null.
when grantResults is null, the exception happed.
============
But I also found this
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
https://developer.android.com/training/permissions/requesting.html
SO this is a bug of the android doc?
Most helpful comment
I just encountered this issue. Is there any known solution?