13
votes

Below here sample code intent from camera :

val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    startActivityForResult(intent, REQUEST_CAMERA)

Note: when I press back from camera as result on Activity result show like this:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=0, data=null} to activity and Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null:

Try to come out solution like this :

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { 
           try {
                when(resultCode){
                    Activity.RESULT_CANCELED -> {
                        System.out.println("nothing")
                    }
                    Activity.RESULT_OK -> {
                        if (requestCode == SELECT_FILE)
                            onSelectFromGalleryResult(data)
                        else if (requestCode == REQUEST_CAMERA)
                            onCaptureImageResult(data)
                    }
                }
            }catch (e:NullPointerException){
                e.printStackTrace()
            }
}

still not solve the problem because when i do debuging log it not come out on func onactivityresult if i go press go back from camera and not capture the image. Taking picture and pickup image from gallery work like charm.

Thank you. Please help me to solve this problem since along way solution given not working. It seem like google have to override fun onresultactivity(resultcode!!:Int) <- this one should have return non null.

2
public void onActivityResult(int requestCode, int resultCode, Intent data) this one i derive from parent fragment. so how to do it in kotlin?Mohd Hafiz
Java to Kotlin converter incorrectly put data: Intent as the param, but it should be nullable, so change it to data: Intent?.user1032613

2 Answers

27
votes

Shouldn't you override this instead?

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }
19
votes

I was getting the same issue while implementing the PayPal payment gateway in my app using the kotlin. You just need to add ? with Intent in onActivityResult as the data can be null if data is null or anything goes wrong. So we need to define data as nullable in onActivityResult

Just replace onActivityResult signature of your SomeActivity with below:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)