0

The google documentation says that I can "call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById()"

This part makes sense:

SelectionFragment fragment = (SelectionFragment) getSupportFragmentManager().findFragmentById(R.id.selectionFragment);

What I don't understand is how I am actually now supposed to call the methods. I've tried everything I can think of. I tried this, and I get a Null Pointer Exception on this line:

String fbID = fragment.userIdView.getText().toString();

I know there are a TON of questions on stack overflow about findFragmentById returning null, but I haven't found a solution in any of them. I have no idea how to identify where the problem is.

To be clear, selectionFragment is the ID of my fragment set in the xml:

<fragment android:name="com.example.androidhive.SelectionFragment"
      android:id="@+id/selectionFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
pkelly
  • 203
  • 2
  • 3
  • 15
  • which part of chain of method calls is null? ie fragment, or fragment.userIdView, etc. – nPn May 20 '14 at 02:47
  • I'm not sure, is there a way to find that out based on the logcat? – pkelly May 20 '14 at 02:51
  • you can always do if (fragment == null) System.out.println("frag is null"); – nPn May 20 '14 at 02:52
  • Thanks. I tried that and it is saying that fragment is null. In that case, should I try one of the solutions involving giving the fragment a tag instead? – pkelly May 20 '14 at 03:01
  • yes, I think I ran into this recently also and that worked. – nPn May 20 '14 at 03:02
  • http://stackoverflow.com/questions/10833666/findfragmentbyid-always-returns-null – nPn May 20 '14 at 03:05
  • Take a look at this question/answer, I found it very helpful when I had this problem. http://stackoverflow.com/questions/10833666/findfragmentbyid-always-returns-null – nPn May 20 '14 at 03:06

0 Answers0