error ArrayIndexOutOfBoundsException occurs only sometimes, user permission?
Solution 1:
When you get an ArrayIndexOutOfBoundsException
it means you are accessing an array index outside the array size. Your stack trace says it happens in onRequestPermissionsResult
, and the only array you access there is grantResults
.
What can sometimes happen is that grantResults
is empty in onRequestPermissionsResult
if the user declines the permissions, so you should check its size before accessing its members
if(grantResults.length == 2 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recreate();
}
else {
// handle permissions not granted
}
Alternately, if you use the new registerForActivityResult
approach you can get around this entirely. Have a look here for more info, or here for an example use.