Fragment getView () always returns null for fragments created by FragmentStatePagerAdapter

I read a lot about fragments. Found other people having problems extracting fragments because null always returned, but the answer did not solve my problem. I am trying to create a gallery of images. I have a fragment that contains an image. To show fragments, I use android.support.v4.view.ViewPager. And to feed the ViewPager I use android.support.v4.app.FragmentStatePagerAdapter .

The problem is that when everything is created and images are displayed, I want to save the current displayed image to disk. Therefore, I need to get the current image of the fragment, but I cannot, because instance.getView () is always null, the activity associated with the fragment is also zero, and I cannot understand why this is so. Here is some code to find out if anyone can help here:

This is my snippet:

 public class ImageViewURLFragment extends Fragment { String _url; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // The last two arguments ensure LayoutParams are inflated // properly. View rootView = new ImageViewURL(getActivity(), _url); return rootView; } public void setImageURL(String imgURL) { _url=imgURL; } } 

And this is my adapter:

 public class ImageViewURLCustomAdapter extends FragmentStatePagerAdapter { List<String> _urls=new ArrayList<String>(); public ImageViewURLCustomAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { ImageViewURLFragment fragment = new ImageViewURLFragment(); fragment.setImageURL(_urls.get(i)); return fragment; } public String getItemUrl(int i) { return _urls.get(i); } @Override public int getCount() { return _urls.size(); } @Override public CharSequence getPageTitle(int position) { return "OBJECT " + (position + 1); } public void addImageURL(String url) { _urls.add(url); } } 

And this is the main activity, pay attention to the comment using get:

 public class MainActivity extends FragmentActivity { ImageViewURLCustomAdapter _iva=null; ViewPager _vp=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); _iva = new ImageViewURLCustomAdapter( getSupportFragmentManager()); _iva.addImageURL("http://upload.wikimedia.org/wikipedia/commons/b/b4/Saturn_(planet)_large.jpg"); _iva.addImageURL("http://planetthreesixty.com/sites/default/files/planet.jpg"); _vp = (ViewPager) findViewById(R.id.viewpager); _vp.setAdapter(_iva); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==R.id.saveToSD) { int index= _vp.getCurrentItem(); Fragment fragment=_iva.getItem(index); //THis is where I need to get fragment view, but always returns null View view= fragment.getView(); ImageView iv=(ImageView)view.findViewWithTag("imageView"); Bitmap bmp=iv.getDrawingCache(); } return super.onOptionsItemSelected(item); } } 

Sorry for the long sources, I cleared everything I could, but wanted to introduce all the game parts. Can anyone guess what hapenning is?

Thanks in advance.

+8
android android-layout android-fragments fragmentstatepageradapter
Feb 09 '13 at 15:18
source share
1 answer

Therefore, I need to get the current image of the fragment, but I can not, because fragment.getView () is always null, the activity associated with the fragment is also zero, and I can not understand why it is.

This is because you expect _iva.getItem(index); will return the Fragment that uses the ViewPager for the page matching the specified index. This will not happen, since the ViewPager has already called the getItem method to get the fragments it needs, and after your call to the getItem method, you will get a new ImageViewURLFragment instance. This new instance is not bound to an Activity ( getActivity() returns null ), and its view has not been created.

As you use the 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); //... 
+31
Feb 09 '13 at 16:03
source share



All Articles