java lang RuntimeException:在Activity上传递结果失败

时间:2017-12-31 12:20:14

标签: android runtimeexception

正如您所看到的,这是我的代码,我构建了几个应用程序,其中我使用了多个权限方法,没有任何问题。在这种情况下,我只有一个活动,但它想要返回请求权限结果时给我一个错误。

代码:

public class ActivityStartUp extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
    private static final String[] PERMISSIONS = { "android.permission.CAMERA",
            "android.hardware.camera", "android.hardware.camera.autofocus",
            "android.hardware.camera.flash",
            "android.permission.ACCESS_COARSE_LOCATION",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.READ_EXTERNAL_STORAGE" ,
            "android.permission.READ_PHONE_STATE"};

    private static final int PERMISSION_CODE = 0xBAB0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);

        if (Utilities.hasMarshmallowApi()) {
            requestPermissions(PERMISSIONS, PERMISSION_CODE);
        }else{
            //Open an activity
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_CODE: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    G.permissionGrantStatus = true;
                    Log.i("Log", "Permission Status:" + true);
                    if(Utilities.hasMarshmallowApi()){
                        // Do something
                    }
                    // Do task
                } else {
                    G.permissionGrantStatus = false;
                    Log.i("Log", "Permission Status:" + false);
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
        }
    }
}

错误:

java.lang.RuntimeException: Failure delivering result 
ResultInfo{who=@android:requestPermissions:, request=47792, result=-1, 
data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} 
to activity {com.bvapp.sh.uni.watermanagement/com.bvapp.sh.uni.watermanagement.ActivityStartUp}: 
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

你可以看到错误" grantResults"长度为0。我试图找到一个很好的解决方案但不幸的是我不能做正确的解决方案。它为什么会发生,我该如何解决? 提前致谢。

1 个答案:

答案 0 :(得分:2)

也许这就是你的问题(来自:OnRequestPermissionsResultCallback() - Android Developer

  

注意:权限请求与用户的交互可能会中断。在这种情况下,您将收到空的   权限和结果数组应该被视为一个   消除。