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. Wh
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) {
int index = _vp.getCurrentItem();
Fragment fragment = _vp.getAdapter().instantiateItem(_vp, index);
//...