java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity
Asked Answered
G

11

75

My app allows the user to press a button, it opens the camera, they can take a photo and it will show up in an ImageView. If the user presses back or cancel while the camera is open I get this force close - Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity... so i am guessing the result=0 is the issue what would I need to insert to make this stop force closing?

Below is my code. I know I am forgetting something but just cant figure it out! (Admittedly I am about 2 weeks into learning android development). Thanks for any help!

private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.photostrippic1);

    ImageView photoButton = (ImageView) this.findViewById(R.id.photostrippic1);



    photoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }

    });

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  

I guess I would need a "else" in there somewhere but I dont exactly know to do that.

below is the logcat

    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity {photo.booth.app/photo.booth.app.PhotoboothActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2934)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2986)
    at android.app.ActivityThread.access$2000(ActivityThread.java:132)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1068)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:4293)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at photo.booth.app.PhotoboothActivity.onActivityResult(PhotoboothActivity.java:76)
    at android.app.Activity.dispatchActivityResult(Activity.java:4108)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2930)
    ... 11 more
        
Gaulin answered 4/12, 2011 at 6:30 Comment(1)
In my case , the error was occurred due to the accidental turning of the option 'Do not keep activities` in developer option :)Zachariahzacharias
B
70

Adding this first conditional should work:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(resultCode != RESULT_CANCELED){
        if (requestCode == CAMERA_REQUEST) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }
    }
}
Bridge answered 27/4, 2012 at 20:43 Comment(3)
you might want to also check that data != null (as well as the RESULT_CANCELED)Evidently
+1 works in fragment and activity, still Safer to check if (data!=null ) , it guarantees the weird stuff of android old versions.Saddlebag
This answer is not relevant to the question. Error in the question says data=null. Hence accessing data without null check fails in the runtime. Actual answer is below by @Manto.Prieto
T
26

For Kotlin Users don't forget to add ? in data: Intent? like

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}
Theocritus answered 25/4, 2018 at 8:13 Comment(0)
S
17

If the user cancel the request, the data will be returned as NULL. The thread will throw a nullPointerException when you call data.getExtras().get("data");. I think you just need to add a conditional to check if the data returned is null.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {
       if (data != null)
       {         
           Bitmap photo = (Bitmap) data.getExtras().get("data"); 
           imageView.setImageBitmap(photo);
       }
}  
Stonyhearted answered 12/7, 2012 at 19:24 Comment(0)
P
9

For Kotlin Users

You just need to add ? with Intent in onActivityResult as the data can be null if user cancels the transaction or anything goes wrong. So we need to define data as nullable in onActivityResult

Just replace onActivityResult signature of SampleActivity with below:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
Puli answered 18/5, 2018 at 11:21 Comment(0)
C
6

I recommend using this:

  1. Retrieve the father Intent.

    Intent intentParent = getIntent();
    
  2. Convey the message directly.

    setResult(RESULT_OK, intentParent);
    

This prevents the loss of its activity which would generate a null data error.

Calendula answered 21/11, 2012 at 21:29 Comment(0)
E
4
 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {  
        if (requestCode == CAMERE_REQUEST && resultCode == RESULT_OK && data != null) 
        {  
           Bitmap photo = (Bitmap) data.getExtras().get("data"); 
           imageView.setImageBitmap(photo);
        } 
}

You can check if the resultCode equals RESULT_OK this will only be the case if a picture is taken and selected and everything worked. This if clause here should check every condition.

Effuse answered 24/6, 2013 at 8:29 Comment(0)
P
2

this is my case

startActivityForResult(intent, PICK_IMAGE_REQUEST);

I defined two request code PICK_IMAGE_REQUESTand SCAN_BARCODE_REQUEST with the same value, eg.

static final int BARCODE_SCAN_REQUEST = 1;

static final int PICK_IMAGE_REQUEST = 1;

this could also causes the problem

Protocol answered 1/5, 2015 at 7:39 Comment(0)
S
1

I had this error message show up for me because I was using the network on the main thread and new versions of Android have a "strict" policy to prevent that. To get around it just throw whatever network connection call into an AsyncTask.

Example:

    AsyncTask<CognitoCachingCredentialsProvider, Integer, Void> task = new AsyncTask<CognitoCachingCredentialsProvider, Integer, Void>() {

        @Override
        protected Void doInBackground(CognitoCachingCredentialsProvider... params) {
            AWSSessionCredentials creds = credentialsProvider.getCredentials();
            String id = credentialsProvider.getCachedIdentityId();
            credentialsProvider.refresh();
            Log.d("wooohoo", String.format("id=%s, token=%s", id, creds.getSessionToken()));
            return null;
        }
    };

    task.execute(credentialsProvider);
Subsidence answered 18/8, 2016 at 10:19 Comment(0)
C
0

I also encountered this question, I solved it through adding two conditions one is:

resultCode != null

the other is:

resultCode != RESULT_CANCELED
Cristincristina answered 23/10, 2013 at 3:43 Comment(2)
how about resultCode != RESULT_OK....? That way you check for all conditions that is not ok, including nullPaperboard
resultCode != null is invalid since resultCode is int !Bova
S
0

My problem was in the called activity when it tries to return to the previous activity by "finishing." I was incorrectly setting the intent. The following code is Kotlin.

I was doing this:

        intent.putExtra("foo", "bar")
        finish()

When I should have been doing this:

        val result = Intent()
        result.putExtra("foo", "bar")
        setResult(Activity.RESULT_OK, result)
        finish()
Shindig answered 29/8, 2017 at 19:46 Comment(0)
S
0

I faced this problem when I tried to pass a serializable model object. Inside that model, another model was a variable but that wasn't serializable. That's why I face this problem. Make sure all the model inside of an model is serializable.

Squatter answered 23/10, 2018 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.