Null pointer error with hideSoftInputFromWindow

后端 未结 14 568
逝去的感伤
逝去的感伤 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 11:48

    As CommonsWare mentioned, the getCurrentFocus() is null, since there is no View component inside the current Activity holding the focus.

    If you already have a view in your Activity, use it to get the window token. For example, if I have a Button component:

    inputManager.hideSoftInputFromWindow(myButton.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    

    Or even worse, if I do not have any view already in my Activity, I could do this:

    inputManager.hideSoftInputFromWindow(new View(this).getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    

    This would solve your problem of NPE, but I hope you find the description useful.

    One more thing about keyboards is that when user presses the back button while the keyboard is visible, the keyboard receives and consumes the back key press to hide itself. Or at least most keyboards behave that way.

    0 讨论(0)
  • 2020-12-25 11:49

    If you want to hide keyboard when touching in the screen, use the below code

           @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.
                INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getRootView().getWindowToken(), 0);
        return true;
    }
    

    If you want to do this in specific view (EditText)

          public void  hideKeyBoard(EditText edt) {
        InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.
                INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);
    
    }
    

    or you can use any View.

    To get the current view

        imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getRootView().getWindowToken(), 0);
    
    0 讨论(0)
  • 2020-12-25 11:49

    The problem happens when there is no element focused, si it is enough with this validation:

    if(activity.getCurrentFocus() != null)
    {
        InputMethodManager inputMethodManager =
        (InputMethodManager) activity.getSystemService(
        Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(
        activity.getCurrentFocus().getWindowToken(), 0);
    }
    
    0 讨论(0)
  • 2020-12-25 11:55

    Everybody above correctly pointed that the getWindowToken() was returning null.

    I was using the default code getCurrentFocus().getWindowToken() to hide keyboard when I encountered the same issue.

    I then realized that as there's no View obtaining the focus I got the NullPointerException.

    We can change the above to:

    anyView.getWindowToken()
    

    where anyView is simply any view in your layout.

    0 讨论(0)
  • 2020-12-25 11:55

    I had the same issue where getCurrentFocus() was returning null. So this method worked for me, and I would just call it on my onClick to hide the keyboard if its shown or still not throw a null pointer exception even if the keyboard was not shown:

    public void hiddenInputMethod() {
    
        InputMethodManager imm = (InputMethodManager) getSystemService(MyActivity.this.INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null)
            imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
    
    0 讨论(0)
  • 2020-12-25 11:59

    I was facing the same issue while hiding the keyboard , i found the simple solution by making root view of layout focusable in touch mode.

    <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:id="@+id/rlSignActivity"
    android:focusableInTouchMode="true"
    xmlns:android="http://schemas.android.com/apk/res/android" >
        .......
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题