I have fragment A:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_main, container, false);
CustomKeyboard customKeyboard = new CustomKeyBoard(getActivity());
etAge = (EditText) getActivity().findViewById(R.id.etAge);
customKeyboard.actionEt(etAge);
return view;
}
And I have class B:
public class CustomKeyboard {
private Context context;
public CustomKeyboard (Context context) {
this.context = context;
}
private void hideKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public void actionEt(Edittext edittext){
editText.setOnFocusChangeListener((view, hasFocus) -> {
if(!hasFocus) {
hideKeyboard(view);
} else {
//........
}
});
}
}
The Exception:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setOnFocusChangeListener(android.view.View$OnFocusChangeListener)' on a null object reference
What is the reason of the problem? Hope for some help.
EDIT: NOW THE APP RUNS. But the Edittexts dont hide.