1
votes

i have one static method in kotlin to hide soft keyboard, it works in java if i pass EditText, TextView as second parameter of method.

But in kotlin it gives error,

Error:(56, 71) Type mismatch: inferred type is EditText? but View was expected

i tried to change View to EditText in function, but it doesn't work for TextView

Also tried to change View to Any, but applicationWindowToken gives error.

This is common function in app.

companion object {
        fun hideSoftKeyboard(activity: Activity, view: View) {
            val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.applicationWindowToken, 0)
        }
    }
1
It means that the argument you are providing to hideSoftKeyboard is not a View, as you defined in the method signature, but a nullable EditText. In addition to @kurt's answer, you can perform a null-check before calling the method - user2340612
am tested your code its not throws any error in android studio - sasikumar
@sasikumar according to the error message he's getting, I'd guess the error is not in the method itself, but on the line where the method is called. Having the code snippet with line 56 (and a few more lines of context) would be helpful to fix the issue - user2340612

1 Answers

2
votes

Try to change code View to View?

companion object {
        fun hideSoftKeyboard(activity: Activity, view: View?) {
            view?.let {
                 val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(it.applicationWindowToken, 0)
            }
        }
    }