今天在一個(gè)Activity隱藏軟鍵盤時(shí)報(bào)Null,錯(cuò)誤提示:
java.lang.NullPointerException: Attempt to invoke virtual
method 'android.os.IBinder android.view.View.getWindowToken()'
on a null object reference
代碼如下:
private void hideEditText() {
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow( MyActivity.this.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);}
修改后正確的代碼:
private void hideEditText() {
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(mEditText.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);}
原因:
還不知道,現(xiàn)在猜測是getWindowToken()的問題。