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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -