隐藏键盘返回空指针
hiding keyboard returning null pointer
我刚收到邮件说我的应用程序崩溃了,我在我的手机上测试了它,但它工作正常,但我不知道为什么它在 OS 9.
context = this;
RelativeLayout parentView = findViewById(R.id.relative_cusweb_parent);
setupParent(parentView);
以上是我的 onCreate 方法 & relative_cusweb_parent 是 CustomWeb Class.[= 的主要相对布局14=]
private void setupParent(View view) {
if (!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideKeyboard();
return false;
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupParent(innerView);
}
}
}
private void hideKeyboard() {
input.clearFocus();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0); // here a is crashing
}
}
应用程序在此行崩溃
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
以下是日志
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at codeline.onlinebills.activities.CustomWeb.hideKeyboard(CustomWeb.java:222)
at codeline.onlinebills.activities.CustomWeb.access0(CustomWeb.java:38)
at codeline.onlinebills.activities.CustomWeb.onTouch(CustomWeb.java:231)
at android.view.View.dispatchTouchEvent(View.java:12611)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3035)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3041)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2671)
你可以用这个方法隐藏键盘:
private void hideKeyBoard()
{
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if( imm != null )
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
尝试用下面的代码替换你的 hideKeyboard 方法,我正在使用这是我的应用程序来隐藏键盘并且它完美地工作:
private void hideKeyboard() {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
它正在崩溃,因为它找不到任何焦点视图和 getCurrentFocus() returns null。
added in API level 1
public abstract View getCurrentFocus ()
Return the view in this Window that currently has focus, or null if there are none. Note that this does not look in any containing Window.
(source)
替换为:
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
有了这个:
imm.hideSoftInputFromWindow(getWindow().getDecorView().getRootView().getWindowToken(), 0);
我刚收到邮件说我的应用程序崩溃了,我在我的手机上测试了它,但它工作正常,但我不知道为什么它在 OS 9.
context = this;
RelativeLayout parentView = findViewById(R.id.relative_cusweb_parent);
setupParent(parentView);
以上是我的 onCreate 方法 & relative_cusweb_parent 是 CustomWeb Class.[= 的主要相对布局14=]
private void setupParent(View view) {
if (!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideKeyboard();
return false;
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupParent(innerView);
}
}
}
private void hideKeyboard() {
input.clearFocus();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0); // here a is crashing
}
}
应用程序在此行崩溃
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
以下是日志
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at codeline.onlinebills.activities.CustomWeb.hideKeyboard(CustomWeb.java:222)
at codeline.onlinebills.activities.CustomWeb.access0(CustomWeb.java:38)
at codeline.onlinebills.activities.CustomWeb.onTouch(CustomWeb.java:231)
at android.view.View.dispatchTouchEvent(View.java:12611)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3035)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3041)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2671)
你可以用这个方法隐藏键盘:
private void hideKeyBoard()
{
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if( imm != null )
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
尝试用下面的代码替换你的 hideKeyboard 方法,我正在使用这是我的应用程序来隐藏键盘并且它完美地工作:
private void hideKeyboard() {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
它正在崩溃,因为它找不到任何焦点视图和 getCurrentFocus() returns null。
added in API level 1
public abstract View getCurrentFocus ()
Return the view in this Window that currently has focus, or null if there are none. Note that this does not look in any containing Window. (source)
替换为:
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
有了这个:
imm.hideSoftInputFromWindow(getWindow().getDecorView().getRootView().getWindowToken(), 0);