简体   繁体   中英

Getting error: override method should call super.onActivityResult

I am trying to capture an image from the camera and display it on the imageView. I tried but getting the error "override method should call super.onActivityResult", you can see my code below. Please let me know if I am doing it right.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
        try{
            val file= File(currentPath)
            val uri = Uri.fromFile(file)

            val imageView = findViewById<ImageView>(R.id.imageView)
            imageView.setImageURI(uri)
        }catch(e:IOException){
            e.printStackTrace()
        }
    }
    if (requestCode == PICK_PICTURE && resultCode == Activity.RESULT_OK) {
        try{
            val uri = data!!.data

            val imageView = findViewById<ImageView>(R.id.imageView)
            imageView.setImageURI(uri)
        }catch(e:IOException){
            e.printStackTrace()
        }
    }
}

add super.onActivityResult(requestCode, resultCode, data); as first line on the onActivityResult method.

What does it mean?

When a class extends another, say you have class A and class B: A

In this context, class A is the super class of B.

So if class A has a method called:

fun someMethod()

you can do:

var myB = B()
myB.someMethod()

this effectively calls the code in A, because B extends A, and someMethod is not private.

Now if you want to modify someMethod 's behavior in B, you override it...

override fun someMethod() {
   // b does something different here
}

now, you can mark A's someMethod as @CallSuper (an annotation coming from here .

In which case you get the warning/error that B must call its super (know as Parent too) class too...

so B must now do:

override fun someMethod() {
   super.someMethod()
   // your B code too..
}

There's no rule to call it at the beginning you can call it at any time, as long as you do before the end of the function. In some instances, it's desired to call it as the first thing (if, for instance, A does something you need in B as well), and in some other cases, you want to wait for B to do something before calling super... . As long as you do.

onActivityResult is marked as such, and therefore you must call super.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM