Null pointer error with hideSoftInputFromWindow

后端 未结 14 569
逝去的感伤
逝去的感伤 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: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();
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-25 12:00

    The warning stoped after I checked that the Input Manager was also != null

    View view = this.getCurrentFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (view != null && imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    
    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
  • 2020-12-25 12:04

    try this:

    public void hideKeyboard(){ 
    InputMethodManager inputManager = (InputMethodManager)            
                this.getSystemService(Context.INPUT_METHOD_SERVICE);    
    inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
    }
    
    0 讨论(0)
  • 2020-12-25 12:05

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-25 12:05

    The main problem is in

    getCurrentFocus().getWindowToken()
    

    Here getcurrentFocus() is the issue. This problem can be solved easily through providing a valid view residing on your current screen like

    yourButtonOnScreen.getWindowToken()
    

    That button wo'nt be null as that is showing on your screen so it will resolve the issue.

    Still if you do'nt have any valid view on your screen the just replace

    getCurrentFocus().getWindowToken()...
    

    with

    //in case of fragment
    
    new View(getActivity()).getWindowToken()
    
    //in case of activity
    
    new View(this).getWindowToken()
    

    hope this will resolve the issue..good luck!

    0 讨论(0)
提交回复
热议问题