0

I have a reference to one of my Fragments obtained by:

Fragment fragmentByTag = getChildFragmentManager().findFragmentByTag(tabName);

Now I need to get it's View, which was setup on its onCreateView() method. I tried:

View view = fragmentByTag.getView();

but the view is always null.

Meaning I have a reference to a Fragment which was correctly instantiated and shown, no doubts. Having a reference to it I need to get a layout associated with it.

Eugene
  • 55,777
  • 85
  • 212
  • 324

2 Answers2

0

There can be a few reasons as to why it is null. I had a similar problem when the view had been destroyed and I was trying to reference it. Are you sure it is on top and accessible/visible?

Can we some more code as to where you are calling this? Have you provided it a layout (i.e. inflated it), and gone through the correct sequence of onAttach(...), onCreate(...), onCreateView(...) and onActivityCreated(...). See the link below for information on that. Showing us these methods may help us solve your problem.

Fragment Creation

JVillella
  • 1,019
  • 1
  • 11
  • 20
0

Your code should work. You'll have to ensure though that you are accessing the getView method after the onCreateView method of the Fragment has returned.

Ameen
  • 2,448
  • 1
  • 12
  • 17
  • Yeah, you're right, the only reason it is null is because I was accessing it before the `onCreateView()` was finished, thanks! – Eugene Sep 17 '13 at 17:35