SmartImagePicker icon indicating copy to clipboard operation
SmartImagePicker copied to clipboard

NullPointerException - Samsung Galaxy S4 - Android 5.0.1

Open er-mo opened this issue 6 years ago • 4 comments

05-27 14:45:15.592 6787-6787/com.myhexaville.androidimagepicker E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myhexaville.androidimagepicker, PID: 6787 java.lang.RuntimeException: Unable to resume activity {com.myhexaville.androidimagepicker/com.myhexaville.androidimagepicker.activity_example.ActivityExample}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=null} to activity {com.myhexaville.androidimagepicker/com.myhexaville.androidimagepicker.activity_example.ActivityExample}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.myhexaville.smartimagepicker.ImagePicker.handleActivityResult(int, int, android.content.Intent)' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3394) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3425) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2763) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4462) at android.app.ActivityThread.access$1000(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=null} to activity {com.myhexaville.androidimagepicker/com.myhexaville.androidimagepicker.activity_example.ActivityExample}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.myhexaville.smartimagepicker.ImagePicker.handleActivityResult(int, int, android.content.Intent)' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:4058) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3380) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3425)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2763)  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4462)  at android.app.ActivityThread.access$1000(ActivityThread.java:177)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:5942)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.myhexaville.smartimagepicker.ImagePicker.handleActivityResult(int, int, android.content.Intent)' on a null object reference at com.myhexaville.androidimagepicker.activity_example.ActivityExample.onActivityResult(ActivityExample.java:31) at android.app.Activity.dispatchActivityResult(Activity.java:6549) at android.app.ActivityThread.deliverResults(ActivityThread.java:4054) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3380)  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3425)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2763)  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4462)  at android.app.ActivityThread.access$1000(ActivityThread.java:177)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:5942)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

er-mo avatar May 27 '18 11:05 er-mo

Same error I am getting in OS 6. Is there any solution?

ashishparmar avatar Jun 06 '18 13:06 ashishparmar

Probably after taking camera image or picking image from gallery, the device was rotated, so the activity is destroyed and the ImagePicker object is null. To solve this issue try to initialize this object (if is null) wherever you want to access to that.

Good luck

Omid3d avatar Jun 08 '18 13:06 Omid3d

This is a problem that many samsung have, as their camera app, rotates to take a picture. I tried to save the state, but it is not a serializable object.

@Omid3d do you have some ideas?

SalvatoreAD avatar Jun 28 '18 08:06 SalvatoreAD

Yes you can't serialize this object and you shouldn't. My recommendation is to recreate object wherever you want to access that. Because after camera rotates you just need to call ImagePicker.handleActivityResult in onActivityResult and I think we can safely get the result from intent. Also think about image pick callback, can we save state of a callback? Maybe no! So the simplest solution is to initialize the object again. For example you can create a method like following:

ImagePicker ip = null;
Uri selectedImageUri = null;
private ImagePicker getImagePicker() {
        if (ip == null) {
            ip = new ImagePicker(this,
                    null,
                    imageUri -> {/*on image picked */
                        selectedImageUri = imageUri;
                    })
                    .setWithImageCrop(
                            16 /*aspect ratio x*/,
                            9 /*aspect ratio y*/);
        }
        return ip;
    }

and then whenever you want to access to this ImagePicker object call this method and instead of serializing ImagePicker object or something else, just save state of selectedImageUri or taken image uri in activity. So after onActivityResult called you have your object and it can handle the result without any problem. Also don't forget to first call this method in oncreate (to ensure you receive callback from ImagePicker).

I hope my answer as a workaround can help you to solve this problem. Good luck

Omid3d avatar Jun 28 '18 09:06 Omid3d