Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null pointer error with hideSoftInputFromWindow

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) 
like image 805
basickarl Avatar asked Sep 28 '13 17:09

basickarl


2 Answers

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);     } } 
like image 82
Maksim Dmitriev Avatar answered Sep 22 '22 08:09

Maksim Dmitriev


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.

like image 37
Armin Avatar answered Sep 24 '22 08:09

Armin