-1

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.

Sarah Pöhler
  • 550
  • 1
  • 7
  • 18

2 Answers2

2

It should be

etAge = (EditText) view.findViewById(R.id.etAge);

Its hard to judge without an XML to look at; to be safe we will add and extra condition.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     view = inflater.inflate(R.layout.fragment_main, container, false);

     etAge = (EditText) view.findViewById(R.id.etAge);
     if(etAge != null){
        CustomKeyboard customKeyboard = new CustomKeyBoard(getActivity());
        customKeyboard.actionEt(etAge);
     }
     return view;
}
Enzokie
  • 7,365
  • 6
  • 33
  • 39
  • The exception will still occur if the Fragment xml doesn't contain that EditText – OneCricketeer Oct 29 '16 at 15:00
  • Yeah thats true and I assume s(he) added an `EditText` and along with the correct ID. – Enzokie Oct 29 '16 at 15:02
  • @SarahPöhler you should ask another question, since the first question you've asked is solved then you should accept it as correct answer. – Enzokie Oct 29 '16 at 15:12
  • lol should I always create a new thread if a new failure happens..... the problem did not solved :( – Sarah Pöhler Oct 29 '16 at 15:20
  • Basically everybody who likes to answer a certain question must cover the OP's question scope (which is you) and its up to them either they want to add extra answer and some helpful tips. While the OP should only expect an answer is within the scope of his/her question. If any issues arises it would be helpful if you create another thread so that broad questions can be sliced into smaller pieces and that would be helpful also for other people that experiencing the same problem. :) – Enzokie Oct 29 '16 at 15:33
1

Your code:

etAge = (EditText) getActivity().findViewById(R.id.etAge);

Is wrong because it inflates from an activity instead of the view it is located in. The cause is comparable to getting a Nullpointer when finding a view in a different layout

Correct code:

etAge = (EditText) view.findViewById(R.id.etAge);

As this finds the EditText in the view instead of the activity(which has no defined view inflated)

Zoe
  • 27,060
  • 21
  • 118
  • 148