hideSoftInputFromWindow出现空指针错误

时间:2013-09-28 17:02:23

标签: android nullpointerexception

我在这一行得到一个空指针异常:

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

从以下方法调用:

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

这是唯一的活动。从片段中按下后退按钮。

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)

14 个答案:

答案 0 :(得分:28)

正如CommonsWare所提到的那样,getCurrentFocus()为空,因为当前View内没有Activity个组成部分。

如果您的Activity中已有视图,请使用它来获取窗口令牌。例如,如果我有一个Button组件:

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

或者更糟糕的是,如果我的活动中没有任何视图,我可以这样做:

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

这可以解决你的NPE问题,但我希望你能找到有用的描述。

关于键盘的另一个问题是,当用户在键盘可见时按下后退按钮时,键盘会接收并消耗后退按键以隐藏自身。或者至少大多数键盘都是这样的。

答案 1 :(得分:22)

我只需要在隐藏键盘之前检查是否有焦点视图。

例如,如果您的活动或片段中有EditText,则可能是焦点视图。当EditText不再集中时,getCurrentFocus()可能会返回null(除非重点关注其他视图)。

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

答案 2 :(得分:20)

以上所有人都正确指出getWindowToken()返回null。

当我遇到同样的问题时,我使用默认代码getCurrentFocus().getWindowToken()来隐藏键盘。

然后我意识到,由于没有View获得焦点,我得到了NullPointerException

我们可以将上述内容更改为:

anyView.getWindowToken()

其中anyView只是布局中的任何视图。

答案 3 :(得分:7)

我遇到了同样的问题,其中getCurrentFocus()返回null。所以这个方法对我有用,我只是在我的onClick上调用它来隐藏键盘,如果它显示或者仍然没有抛出空指针异常,即使键盘没有显示:

public void hiddenInputMethod() {

    InputMethodManager imm = (InputMethodManager) getSystemService(MyActivity.this.INPUT_METHOD_SERVICE);
    if (getCurrentFocus() != null)
        imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

答案 4 :(得分:4)

如果您想在屏幕上触摸时隐藏键盘,请使用以下代码

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

如果您想在特定视图(EditText)中执行此操作

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

}

或者您可以使用任何视图。

获取当前视图

    imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getRootView().getWindowToken(), 0);

答案 5 :(得分:2)

这对我有用。只需添加以下getWindow()。getDecorView()。getRootView()。getWindowToken()即可,而不是使用getCurrectFocus()。之后,您可以在活动中的任何位置使用此方法。

public static void hideSoftKeyboard(Activity activity) {
        InputMethodManager inputMethodManager =
                (InputMethodManager) activity.getSystemService(
                        Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(
                activity.getWindow().getDecorView().getRootView().getWindowToken(), 0);
    }

答案 6 :(得分:1)

如果收到错误,请使用此命令:由以下引起: java.lang.NullPointerException:尝试调用虚拟方法' android.os.IBinder android.view.View.getWindowToken()'在空对象引用上

InputMethodManager inputMethodManager = (InputMethodManager) MainActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);

   if(MainActivity.this.getCurrentFocus() != null)
        {
         inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().
getWindowToken(), 0);
        }

答案 7 :(得分:1)

主要问题在于

getCurrentFocus().getWindowToken()

此处 getcurrentFocus()是个问题。 通过提供驻留在当前屏幕上的有效视图(如

),可以轻松解决此问题
yourButtonOnScreen.getWindowToken()

该按钮不会显示在屏幕上,因此它会解决问题。

如果你的屏幕上没有任何有效的视图,那么只需替换

getCurrentFocus().getWindowToken()...

<强>与

//in case of fragment

new View(getActivity()).getWindowToken()

//in case of activity

new View(this).getWindowToken()

希望这会解决问题。好运!

答案 8 :(得分:0)

我在隐藏键盘的同时遇到了同样的问题,我通过在触摸模式下调整布局的根视图找到了简单的解决方案。

<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>

答案 9 :(得分:0)

如果InputMethodManager的实例为null,则可能显示错误。试试下面的代码吧。

void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) 
getActivity().getSystemService(
        Context.INPUT_METHOD_SERVICE);
View focusedView = getActivity().getCurrentFocus();

if (focusedView != null) {

    try{
    assert inputManager != null;
    inputManager.hideSoftInputFromWindow(focusedView.getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);
}catch(AssertionError e{
     e.printStackTrace();
    }
  }
}

答案 10 :(得分:0)

当没有重点关注元素时就会发生问题,因为使用此验证就足够了:

if(activity.getCurrentFocus() != null)
{
    InputMethodManager inputMethodManager =
    (InputMethodManager) activity.getSystemService(
    Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(
    activity.getCurrentFocus().getWindowToken(), 0);
}

答案 11 :(得分:0)

在我检查输入管理器也为!= null

后,警告停止了
View view = this.getCurrentFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (view != null && imm != null) {
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

答案 12 :(得分:-2)

试试这个:

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

答案 13 :(得分:-2)

只需使用这个:

public void hideKeyboard(View v){ 
  InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}