android - Fragment.getView() always return null -
i add 2 fragments viewpager in mainactivity dynamically,while i'm trying sub view of fragments, fragment.getview() return null, how can solve problem? in advance.
mlinearlayout= (linearlayout)fragments.get(0).getview().findviewbyid(r.id.linear_layout); mrelativelayout= (relativelayout) fragments.get(1).getview().findviewbyid(r.id.relative_layout);
if you, use fragments' oncreateview()
bind views, let parent activity
know views through interface in onactivitycreated()
.
your interface like
public interface viewinterface { void onlinearlayoutcreated(linearlayout layout); void onrelativelayoutcreated(relativelayout layout); }
and in each fragment
public view oncreateview (layoutinflater inflater, viewgroup container, bundle savedinstancestate) { viewgroup layout = (viewgroup) inflater.inflate(r.layout.fragment_layout, inflater, false); mlinearlayout = layout.findviewbyid(r.id.linear_layout); ... return layout; } ... public void onactivitycreated (bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); try { viewinterface callback = (viewinterface) getactivity(); callback.onlinearlayoutcreated(mlinearlayout); } catch (classcastexception e) { log.e("error", getactivity().getname()+" must implement viewinterface"); } ... }
and in parent activity
implements viewinterface
void onlinearlayoutcreated(linearlayout layout) { //do linearlayout ... }
Comments
Post a Comment