Null pointer error with hideSoftInputFromWindow

basickarl picture basickarl · Sep 28, 2013 · Viewed 37.5k times · Source

I get a null pointer exception at this row:

public void hideKeyboard(){ 
InputMethodManager inputManager = (InputMethodManager)            
            this.getSystemService(Context.INPUT_METHOD_SERVICE);    
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),      
            InputMethodManager.HIDE_NOT_ALWAYS);
}

This is called from the following method:

@Override
public void onBackPressed() {
    super.onBackPressed();
    hideKeyboard();
}

This is in the only activity. The back button is pressed from a fragment.

STACK:

09-28 19:14:40.301: E/InputEventSender(30324): Exception dispatching finished signal.
09-28 19:14:40.301: E/MessageQueue-JNI(30324): Exception in MessageQueue callback: handleReceiveCallback
09-28 19:14:40.325: E/MessageQueue-JNI(30324): java.lang.NullPointerException
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at com.example.ecohelp.MainActivity.hideKeyboard(MainActivity.java:75)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at com.example.ecohelp.MainActivity.onBackPressed(MainActivity.java:31)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.app.Activity.onKeyUp(Activity.java:2159)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.KeyEvent.dispatch(KeyEvent.java:2647)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.app.Activity.dispatchKeyEvent(Activity.java:2389)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.os.MessageQueue.nativePollOnce(Native Method)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.os.MessageQueue.next(MessageQueue.java:132)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.os.Looper.loop(Looper.java:124)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at android.app.ActivityThread.main(ActivityThread.java:5103)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at java.lang.reflect.Method.invokeNative(Native Method)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at java.lang.reflect.Method.invoke(Method.java:525)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-28 19:14:40.325: E/MessageQueue-JNI(30324):  at dalvik.system.NativeStart.main(Native Method)

Answer

Maksim Dmitriev picture Maksim Dmitriev · Sep 18, 2014

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);
    }
}