Null pointer error with hideSoftInputFromWindow

后端 未结 14 738
逝去的感伤
逝去的感伤 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 12:01

    This works for me. Simply add the getWindow().getDecorView().getRootView().getWindowToken() following instead of using getCurrectFocus(). After that You can use this method for any where in your activity.

    public static void hideSoftKeyboard(Activity activity) {
            InputMethodManager inputMethodManager =
                    (InputMethodManager) activity.getSystemService(
                            Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(
                    activity.getWindow().getDecorView().getRootView().getWindowToken(), 0);
        }
    

提交回复
热议问题