Null pointer error with hideSoftInputFromWindow

后端 未结 14 711
逝去的感伤
逝去的感伤 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

    Everybody above correctly pointed that the getWindowToken() was returning null.

    I was using the default code getCurrentFocus().getWindowToken() to hide keyboard when I encountered the same issue.

    I then realized that as there's no View obtaining the focus I got the NullPointerException.

    We can change the above to:

    anyView.getWindowToken()
    

    where anyView is simply any view in your layout.

提交回复
热议问题