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) {
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) {
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.
android android-layout android-fragments fragmentstatepageradapter
Notbad Feb 09 '13 at 15:18 2013-02-09 15:18
source share