Null pointer error with hideSoftInputFromWindow

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

I get a null pointer exception at this row:

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


        
14条回答
  •  萌比男神i
    2020-12-25 12:00

    It can show error if instance of InputMethodManager is null. Try the below code it worked for me.

    void hideKeyboard() {
    InputMethodManager inputManager = (InputMethodManager) 
    getActivity().getSystemService(
            Context.INPUT_METHOD_SERVICE);
    View focusedView = getActivity().getCurrentFocus();
    
    if (focusedView != null) {
    
        try{
        assert inputManager != null;
        inputManager.hideSoftInputFromWindow(focusedView.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }catch(AssertionError e{
         e.printStackTrace();
        }
      }
    }
    

提交回复
热议问题