Failure delivering result - Cannot execute non-voi

2019-08-12 13:26发布

问题:

I'm using EasyPermissions library from Google to handle Permissions on Android 6. The first time I click the button to capture image it asks me to give permissions for CAMERA and for WRITE_EXTERNAL_STORAGE.

After I have accepted both permissions app crashes with the error message you can see below :

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) 

and the openCamera() method :

@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);
    }
}

回答1:

You get that exception because EasyPermission can't invoke your method openCamera since it has the parameter option. The methods annotated with AfterPermissionGranted must be void so EasyPermission can invoke them.

This condition is explicitly verified in the source of 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());
}

To resolve the issue you must remove any parameter from the method openCamera.