Null pointer error with hideSoftInputFromWindow

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

    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!

提交回复
热议问题