可能出现此问题的原因是,你在Activity中,调用adapter.getItem()来获取当前Fragment。
大概如下
这是因为,这个Adapter.getItem方法返回的是一个新的Fragment实例,并不是当前的Fragment。
原文解释如下:
That is happening because you're expecting _myFragmentAdapter.getItem(index); to return the Fragmentthat 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.
解决方法,用如下代码来获取一个当前可见的Fragment:
本文探讨了在使用Fragment时,调用Fragment.getView()总是返回null的原因及解决方案。主要原因是Adapter.getItem()方法返回的是一个新的Fragment实例,而不是当前的Fragment。为了解决这个问题,可以通过调用ViewPager的getAdapter().instantiateItem()方法来获取当前可见的Fragment。
1187

被折叠的 条评论
为什么被折叠?



