0

I have a form in fragment A, after the user enters their information and presses on submit, they should get redirected to fragment B.

I can manage to call the fragment method, however I'm getting a NullPointerException error:

Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

How I am calling the method in fragment B:

(new FragmentB()).infoSubmitted();

How could I effectively call the method in fragment B?

Boron
  • 443
  • 4
  • 20
  • Your error indicated that _You are using context which is null_ – Piyush May 17 '19 at 07:55
  • Because `new FragmentB()` is not attached to Activity . So you need to get the reference of fragment which is currently attached .. [See this](https://stackoverflow.com/questions/9294603/how-do-i-get-the-currently-displayed-fragment) if a Layout is a Container.. – ADM May 17 '19 at 08:22

1 Answers1

1

You can do that way, but it isn't good approach.

Once you submit form in FragmentA, return results to Activity and then start FragmentB.

In FragmentA it would be like:

public void formSubmitted(){
     getActivity().startFragmentB(mFormData)
}

And in Activivity create a method:

public void startFragmentB(Form formData){
     // handle fromData
     // start fragmentB
}
Said
  • 600
  • 5
  • 18