Null pointer error with hideSoftInputFromWindow

后端 未结 14 712
逝去的感伤
逝去的感伤 2020-12-25 11:32

I get a null pointer exception at this row:

public void hideKeyboard(){ 
InputMethodManager inputManager = (InputMethodManager)            
            this.         


        
14条回答
  •  天命终不由人
    2020-12-25 11:55

    I had the same issue where getCurrentFocus() was returning null. So this method worked for me, and I would just call it on my onClick to hide the keyboard if its shown or still not throw a null pointer exception even if the keyboard was not shown:

    public void hiddenInputMethod() {
    
        InputMethodManager imm = (InputMethodManager) getSystemService(MyActivity.this.INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null)
            imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
    

提交回复
热议问题