M
M
Masteron2016-06-13 17:41:42
Java
Masteron, 2016-06-13 17:41:42

Fragments in android?

There is a code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re);
        fragmentManager = getFragmentManager();
        if(getIntent().getExtras().get(keyMessage).equals("R")) registration();
        else
        if(getIntent().getExtras().get(keyMessage).equals("E")) enter();
        else Toast.makeText(ReActivity.this, "Error01 Please restart the application", Toast.LENGTH_SHORT).show();
    }

In which, depending on what the past activity passed, the method is called.
And then the method itself:
private void enter(){
        EnterFragment entFragment = new EnterFragment();
        fragmentManager.beginTransaction()
                .add(R.id.fragmentRE,entFragment)
                .commit();
        entFragment.getView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

The problem is nullPointerExpection where we want to get a listener from the button
(The button itself is present on the fragment with the same id as in the code)
Tell me where I made a mistake and how to fix it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2016-06-13
@Rou1997

Layout at that moment is simply not loaded yet, you need to do it in the Fragment itself, in onCreateView, after inflate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question