android - Fragment getView() always returning null for Fragments created by a FragmentStatePagerAdapter -
i have been reading lot fragments. have found other people having problems retrieving fragments view because null returned no reply solved problem. i'm trying create image gallery. have fragment holds image view. show fragments utilize android.support.v4.view.viewpager. , feed viewpager
utilize android.support.v4.app.fragmentstatepageradapter
.
the problem when build , images shown want save current shown image disk. need current fragment imageview can't because fragment.getview() null, activity associated fragment null , can't figure out why it. here code see if help here:
this fragment:
public class imageviewurlfragment extends fragment { string _url; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // lastly 2 arguments ensure layoutparams inflated // properly. view rootview = new imageviewurl(getactivity(), _url); homecoming rootview; } public void setimageurl(string imgurl) { _url=imgurl; } }
and 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)); homecoming fragment; } public string getitemurl(int i) { homecoming _urls.get(i); } @override public int getcount() { homecoming _urls.size(); } @override public charsequence getpagetitle(int position) { homecoming "object " + (position + 1); } public void addimageurl(string url) { _urls.add(url); } }
and main activity, notice comment 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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { if(item.getitemid()==r.id.savetosd) { int index= _vp.getcurrentitem(); fragment fragment=_iva.getitem(index); //this need fragment view, returns null view view= fragment.getview(); imageview iv=(imageview)view.findviewwithtag("imageview"); bitmap bmp=iv.getdrawingcache(); } homecoming super.onoptionsitemselected(item); } }
sorry long sources, have cleaned could, wanted nowadays playing parts. guess hapenning?
thanks in advance.
so need current fragment imageview can't because fragment.getview() null, activity associated fragment null , can't figure out why it.
that happening because you're expecting _iva.getitem(index);
homecoming fragment
viewpager
uses page corresponding specified index. not happen viewpager
has called getitem
method fragments needs , after phone call getitem
method new imageviewurlfragment
instance. new instance isn't tied activity
(getactivity()
returns null
) , view wasn't created.
as utilize fragmentstatepageradapter
seek code below visible fragment
:
if (item.getitemid()==r.id.savetosd) { int index = _vp.getcurrentitem(); fragment fragment = _vp.getadapter().instantiateitem(_vp, index); //...
android android-layout android-fragments fragmentstatepageradapter
No comments:
Post a Comment