嗨,我正在尝试输入这段代码来隐藏 Android 上的软键盘,但它返回一个空指针。
代码:
public void testSetTestEnvironment (){
solo.clickInList(4);
solo.clickOnMenuItem(ConfigVariables.CATALOGSERVER);
assertTrue(solo.searchText(ConfigVariables.CATALOGSERVERURL));
//Enter KeyCode
solo.clickInList(5);
View myEditText = solo.getViews().get(0);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
solo.enterText(solo.getEditText(0), "");
solo.enterText(0, ConfigVariables.KEYCODE);
assertTrue(popupClickButtonHandler("Enter KeyCode", "OK"));
日志显示:
java.lang.NullPointerException
谢谢。
最佳答案
也许有点太晚了,但我希望这对某人有帮助。 您的代码很容易出现错误,因为它依赖于聚焦 View 来获取窗口 token 。
尝试使用:
InputMethodManager im = (InputMethodManager) this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
这段代码对我来说工作正常。
取自 this answer通过 venator85
关于java - 使用InputMethodManager时给出空指针 imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10942152/