Skip to content Skip to sidebar Skip to footer

Fragment Getview() Always Returning Null For Fragments Created By A Fragmentstatepageradapter

I have been reading a lot about fragments. Have found other people having problems retrieving fragments view because null was always returned but no answer solved my problem. What

Solution 1:

So I need to get the current fragment imageview but I can't because fragment.getView() always is null, the activity associated with the fragment is null too and I can't figure out why is it.

That is happening because you're expecting _iva.getItem(index); to return the Fragment that the ViewPager uses for the page corresponding to the specified index. That will not happen as the ViewPager has already called the getItem method to get the fragments it needs and after your call the getItem method you get a new ImageViewURLFragment instance. This new instance isn't tied to the Activity(getActivity() returns null) and its view wasn't created.

As you use a FragmentStatePagerAdapter try the code below to get the currently visible Fragment:

if (item.getItemId()==R.id.saveToSD) {
     intindex = _vp.getCurrentItem();
     Fragment fragment = _vp.getAdapter().instantiateItem(_vp, index);
     //...

Post a Comment for "Fragment Getview() Always Returning Null For Fragments Created By A Fragmentstatepageradapter"