I get a null pointer exception at this row:
public void hideKeyboard(){
InputMethodManager inputManager = (InputMethodManager)
this.
I just need to check if there is a focused view before hiding the keyboard.
For example, if you have an EditText
in your activity or fragment, it'll probably be the focused view. When the EditText
isn't focused anymore, getCurrentFocus()
may return null (unless some other view is focused).
void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
View focusedView = getActivity().getCurrentFocus();
/*
* If no view is focused, an NPE will be thrown
*
* Maxim Dmitriev
*/
if (focusedView != null) {
inputManager.hideSoftInputFromWindow(focusedView.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}