Search code examples
androidkotlinandroid-intentcameraandroid-camera-intent

onActivityResult() "accidental override" error


So i'm making this app, that captures an image, and that image is used further to extract text from it. I found this code, to access the camera and capture image (in a fragment):

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!

    private val CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1888
    var button: Button? = null
    var imageView: ImageView? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        button!!.setOnClickListener {
            fun onClick(view: View?) {
                val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                startActivityForResult(
                    intent,
                    CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE
                )
            }
        }
        return root
    }
    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                val bmp = data.extras!!["data"] as Bitmap?
                val stream = ByteArrayOutputStream()
                bmp!!.compress(Bitmap.CompressFormat.PNG, 100, stream)
                val byteArray: ByteArray = stream.toByteArray()

                // convert byte array to Bitmap
                val bitmap = BitmapFactory.decodeByteArray(
                    byteArray, 0,
                    byteArray.size
                )
                imageView!!.setImageBitmap(bitmap)
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

But there is an "accidental override" error with onActivityResult() method, and i don't know how to fix that: error message

I've tried adding this to MainActivity class, hoping for the best:

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

But it doesn't seem to help. I am a total beginner, but i've got to make this app real quick, so will be grateful for any advice or explanation.

upd: I've also tried adding keyword override, but i get error "onActivityResult() overrides nothing"


Solution

  • Your definition of OnActivityResult() isn't correct. It needs to have the same signature as the method you are trying to override. You need this:

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

    You need the question mark on the last parameter type Intent? because the value of this parameter may be null. This will match the signature of the method from Fragment that you are trying to override.