I am new to Kotlin and I have an error like this in my onActivityResult method
'Type mismatch: inferred type is Intent? but Intent was expected'
 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode  != REQUEST_CODE)
            {
                return
            }
        if (resultCode != Activity.RESULT_OK)
        {
            Toast.makeText(this,"Screen cast permission denied",Toast.LENGTH_LONG).show()
            return
        }
        mediaProjectionCallBack = MediaProjectionCallback()
        mediaProjection = projectionManager!!.getMediaProjection(resultCode,data)
        mediaProjection!!.registerCallback(mediaProjectionCallBack,null)
        virtualDisplay = createVirtualDisplay()
        mediaRecorder!!.start()
    }
Error is in this line when I use data from onActivity result it gives me an error which I mentioned above:
mediaProjection = projectionManager!!.getMediaProjection(resultCode,data)
In Kotlin there are nullable types, Intent? and Intent aren't the same type.
data:Intent? means that your data can be null.
getMediaProjection(resultCode:Int, data:Intent) method wants your data to be of type Intent which means it can't accept null values. So you have to either cast it to not-null value with !! operator or check if it is not null:
if(data != null){
    mediaProjection = projectionManager!!.getMediaProjection(resultCode,data)
}
In this case automatically knows that data is not null, so it doesn't throw error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With