Null pointer error with hideSoftInputFromWindow

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

    Just use this one:

    public void hideKeyboard(View v){ 
      InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    
    0 讨论(0)
  • 2020-12-25 12:06

    Use this if you are getting error : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.IBinder android.view.View.getWindowToken()' on a null object reference


    InputMethodManager inputMethodManager = (InputMethodManager) MainActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
    
       if(MainActivity.this.getCurrentFocus() != null)
            {
             inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().
    getWindowToken(), 0);
            }
    
    0 讨论(0)
提交回复
热议问题