传递结果失败 - 无法执行非空方法openCamera

mt0*_*t0s 1 android android-permissions

我使用EasyPermissions从谷歌库来处理Android上6.第一次权限我按一下按钮,捕捉图像,它要求我给权限CAMERAWRITE_EXTERNAL_STORAGE.

在我接受了两个权限应用程序崩溃并显示错误消息后,您可以在下面看到:

E/AndroidRuntime: FATAL EXCEPTION: main
                    Process: com.example.debug, PID: 22768
                    java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=123, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example.debug/com.example.camera.CameraActivity}: java.lang.RuntimeException: Cannot execute non-void method openCamera
                        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:5417)
                        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.RuntimeException: Cannot execute non-void method openCamera
                        at pub.devrel.easypermissions.EasyPermissions.runAnnotatedMethods(EasyPermissions.java:229)
                        at pub.devrel.easypermissions.EasyPermissions.onRequestPermissionsResult(EasyPermissions.java:186)
                        at com.example.camera.CameraActivity.onRequestPermissionsResult(CameraActivity.java:243)
                        at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553)
                        at android.app.Activity.dispatchActivityResult(Activity.java:6432)
                        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:5417) 
                        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) 
Run Code Online (Sandbox Code Playgroud)

openCamera() 方法:

@Override
@AfterPermissionGranted(RC_CAMERA_PERM)
public void openCamera(int option) {
    if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        // Have permission, do the thing!
        Log.i(TAG, "openCamera Has Permissions ");

        Intent intent;

        switch (option) {
            case RECORD_VIDEO :
                intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, FIVE_MINS_IN_SECS);
                startActivityForResult(intent, RECORD_VIDEO);
                break;
            case CAPTURE_IMAGE :
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, "Image File name");
                mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
                startActivityForResult(intent, CAPTURE_IMAGE);
                break;
            default:
                Log.i(TAG, "openCamera wrong option ");
                break;
        }

    } else {
        // Ask for Camera permission
        EasyPermissions.requestPermissions(this, getString(R.string.ask_camera_permission),
                RC_CAMERA_PERM, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*ini 7

您得到该异常是因为EasyPermission无法调用您的方法,openCamera因为它具有参数option.注释的方法AfterPermissionGranted必须为void,因此EasyPermission可以调用它们.

这个条件在EasyPermission的源代码中明确验证:

// Method must be void so that we can invoke it
if (method.getParameterTypes().length > 0) {
    throw new RuntimeException("Cannot execute non-void method " + method.getName());
}
Run Code Online (Sandbox Code Playgroud)

要解决此问题,您必须从方法中删除任何参数openCamera.