在Android牛轧糖上调用onRequestPermission时崩溃java.lang.ArrayIndexOutOfBoundsException

时间:2018-08-19 12:06:36

标签: android permissions

所以我的应用程序在所有设备上都能正常运行,但是今天我收到了一个非常令人困惑的崩溃报告。

我有1个活动,2个片段。

HomeFragment 是导致此崩溃的一个。

这是当机报告:

java.lang.ArrayIndexOutOfBoundsException: 

  at mypackage.myappname.HomeFragment.onRequestPermissionsResult (HomeFragment.java:432)

  at android.support.v4.app.FragmentActivity.onRequestPermissionsResult (FragmentActivity.java:804)

  at android.app.Activity.requestPermissions (Activity.java:4296)

  at android.support.v4.app.ActivityCompat.requestPermissions (ActivityCompat.java:507)

  at android.support.v4.app.FragmentActivity.requestPermissionsFromFragment (FragmentActivity.java:894)

  at android.support.v4.app.FragmentActivity$HostCallbacks.onRequestPermissionsFromFragment (FragmentActivity.java:953)

  at android.support.v4.app.Fragment.requestPermissions (Fragment.java:1132)

  at mypackage.myappname.HomeFragment.onPickClicked (HomeFragment.java:339)

  at mypackage.myappname.HomeFragment.onClick (HomeFragment.java:1150)

  at android.view.View.performClick (View.java:6261)

  at android.view.View$PerformClick.run (View.java:23752)

  at android.os.Handler.handleCallback (Handler.java:751)

  at android.os.Handler.dispatchMessage (Handler.java:95)

  at android.os.Looper.loop (Looper.java:154)

  at android.app.ActivityThread.main (ActivityThread.java:6776)

  at java.lang.reflect.Method.invoke (Native Method)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1518)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)

以下几行是重要的部分:

  at mypackage.myappname.HomeFragment.onRequestPermissionsResult (HomeFragment.java:432)


  at android.support.v4.app.ActivityCompat.requestPermissions (ActivityCompat.java:507)



  at mypackage.myappname.HomeFragment.onPickClicked (HomeFragment.java:339)

  at mypackage.myappname.HomeFragment.onClick (HomeFragment.java:1150)

在第432行,我处理了onRequestPermissionResult:

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


        switch (requestCode) {

            case GALLERY_PERMISSIONS_REQ_CODE:

                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                        openGallery();
                }
                imgGalleryCV.setClickable(true);
                break;


        }
    }

,在第339行,我在此处调用requestPermission:

requestPermissions(galleryPermission,GALLERY_PERMISSIONS_REQ_CODE);
        imgGalleryCV.setClickable(false);

我的String [] galleryPermission:

private final static String[] galleryPermission = {Manifest.permission.READ_EXTERNAL_STORAGE};

我认为我的代码没有什么问题。 那么是bug还是android只是想让我感到困惑?

2 个答案:

答案 0 :(得分:1)

您需要像这样检查GrantResults大小是否大于0

  if (grantResults.length > 0){

                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                        openGallery();
                }
                imgGalleryCV.setClickable(true);

}

答案 1 :(得分:1)

调用父方法read(byte[]) 只是与您收到的请求代码不匹配:

super.onRequestPermissionsResult(requestCode,permissions,grantResults);

这里有一个RuntimePermissions用法示例:https://github.com/googlesamples/android-RuntimePermissions/blob/master/Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java