I have code in Java that hides the soft keyboard using the InputMethodManager. When I convert the code to Kotlin, the same code throws a NoMethodFound exception.
I can easily switch between the Java and Kotlin versions and demonstrate the correct behaviour in Java and incorrect behaviour in Kotlin.
Java code
searchText.clearFocus();
InputMethodManager imm = (InputMethodManager)dialog.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
try {
imm.hideSoftInputFromWindow(searchText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Throwable t) {
String stop = "here";
}
Kotlin code
searchText!!.clearFocus()
val imm = dialog!!.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
try {
imm.hideSoftInputFromWindow(searchText!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
} catch (t: Throwable) {
val stop = "here"
}
The Java code exhibits the correct behaviour and dismisses the soft keyboard. The Kotlin code throws the exception
"java.lang.NoSuchMethodError: No virtual method hideSoftInputFromWindow(Landroid/os/IBinder;I)V in class Landroid/view/inputmethod/InputMethodManager; or its super classes (declaration of 'android.view.inputmethod.InputMethodManager' appears in /system/framework/framework.jar:classes2.dex)"
It look like this method not available in
Context. Try useContextfrom your application Context. For getting Context of application do something like this or some googling about getting application in kotlin may help.