I just received fabric mail that my app crashed and I test it on my mobile but it's working fine but I don't why it crashes on OS 9.
context = this;
RelativeLayout parentView = findViewById(R.id.relative_cusweb_parent);
setupParent(parentView);
Above is my onCreate method & relative_cusweb_parent is main relative layout of CustomWeb Class.
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
}
}
App is crashing on this line
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
and below is the log
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.access$200(CustomWeb.java:38)
at codeline.onlinebills.activities.CustomWeb$4.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)
It's crashing because it doesn't find any view in focus and 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)
Replace this:
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
with this:
imm.hideSoftInputFromWindow(getWindow().getDecorView().getRootView().getWindowToken(), 0);