Strange ArrayIndexOutOfBoundsException in android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback

后端 未结 2 743
逝去的感伤
逝去的感伤 2021-02-06 22:51

I received a strange out of bounds exception in the Play Store console relating to the android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback

j         


        
相关标签:
2条回答
  • 2021-02-06 23:17

    Kotlin code for check Permission & Fix this error: It's better to check size of IntArray with .isNotEmpty() in Kotlin.

     override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<out String>,
        grantResults: IntArray
    ) {
        when (requestCode) {
            PERMISSIONS_REQUEST_PICK_IMAGE_GALLERY -> {
                if (grantResults.isNotEmpty()  &&  grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    choosePhotoFromGallary()
                } else  //Permission Denied
                    //  toast("Permission must be granted in order to...")
            }
            PERMISSIONS_REQUEST_TAKE_PICTURE_CAMERA -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    takePhotoFromCamera()
                } else  //Permission Denied
                    //  toast("Permission must be granted in order to ...")
            }
        } // when
    } // fun
    
    0 讨论(0)
  • 2021-02-06 23:36

    Documentation about permissions says

    If request is cancelled, the result arrays are empty.

    And it seems to be your situation, so you have to check first if the array is not empty:

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_LOCATION_PERMISSIONS:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                    }
                    startLocationUpdates();
                } else {
                    // Permission Denied
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    
    0 讨论(0)
提交回复
热议问题