运行时权限:将结果ResultInfo {who = @ android:requestPermissions:,request = 123,result = 0,data = null}发送到活动

时间:2016-09-28 00:41:52

标签: android permissions android-permissions

我正在尝试获取READ_EXTERNAL_STORAGE权限。这是我的代码

    // Check if user granted the permission
    int hasReadExternalStoragePermission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE);


   if(hasReadExternalStoragePermission == PackageManager.PERMISSION_GRANTED) {
       displaySongs();
   } else {
       // Should we show explanation?
        if( ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)) {
               // Show explanation to user
        } else {

            // No explanation needed. We can request permissions
            String permissions[] = {
                    Manifest.permission.READ_EXTERNAL_STORAGE
            };
            ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSIONS_CODE);
        }
   }

这是回叫方法

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

    switch (requestCode) {
        case REQUEST_PERMISSIONS_CODE:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                displaySongs();
            } else {
                // Permission Denied
                Toast.makeText(MainActivity.this, "Permissions denied", Toast.LENGTH_SHORT)
                        .show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
  • 我正在Nexus 6P上调试我的应用程序。
  • 目标和编译的SDK版本为24。
  • 当我运行应用程序时,它不会显示任何权限提示。它只是崩溃了。

这是我在Android监视器中收到的错误消息

    09-27 20:05:48.143 31106-31106/org.javacad.music E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: org.javacad.music, PID: 31106
                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=123, result=0, data=null} to activity {org.javacad.music/org.javacad.music.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                   at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                   at org.javacad.music.MainActivity.onRequestPermissionsResult(MainActivity.java:129)
                                                                   at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582)
                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:6460)
                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                   at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  • 我没有检查"再也没有问过"复选框。
  • 我没有否认许可请求。
  • 我已经将权限定义为AndoridManifest.xml

    中manifest元素的直接子元素
     <!--READ_EXTERNAL_PERMISSION to read files form storage-->
    <user-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    • 为什么没有显示权限对话框?
    • 为什么我在回调方法中获得空权限和grantResults数组?

1 个答案:

答案 0 :(得分:6)

在我的情况下,由于我试图在onRequestPermissionsResult内立即显示片段,因此发生了此异常。添加延迟修复了问题。

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case CAMERA_REQ_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   new Handler().postDelayed(new Runnable() {
                       @Override
                       public void run() {
                           showFragment(QRCodeFragment.newInstance()); //Fragment transaction code 
                       }
                   }, 500);
                } else {
                    Toast.makeText(this, "Please grant camera permission to use the QR Scanner", Toast.LENGTH_SHORT).show();
                }
        }
    }