android - Am I using findFragmentByTag Correctly -


my activity creates fragments dynamically , not created right off start(hidden). need able identify fragments can send data , them activity. keep getting null object reference when trying call fragment's function (addathletetolist) main activity. says athlete object null identifying(creating) fragment references correctly? thanks

if not, how create tags fragments?

main activity function sends data fragment b (athleteslist):

 // interface function  // sends athlete information athlete list  @override public void send(athlete athlete) {     log.e("", "main activity: " + athlete.getfirstname());     athleteslist athleteslist = (athleteslist) getsupportfragmentmanager().findfragmentbytag("athletelist");      arraylist<string> athleteevents = new arraylist<string>();     athleteevents = athlete.getevents();      for(int = 0; < athleteevents.size(); i++) {         log.e("", "athlete event: " + athleteevents.get(i).tostring());         switch (athleteevents.get(i).tostring()){             case "4 x 400":                 athleteslist.addathletetolist(athlete, 1);                 break;             case "1-mile":                 break;             case "100-meter":                 break;         }     } } 

fragment a:

@override public void onactivitycreated(@nullable bundle savedinstancestate) {     super.onactivitycreated(savedinstancestate);     btnaddathlete.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             athlete athlete = new athlete();             athlete.setfirstname(editfirstname.gettext().tostring());             athlete.setlastname(editlastname.gettext().tostring());             athlete.setage(integer.parseint(editage.gettext().tostring()));             athlete.setevents(athleteevents);             athlete.setgrade(editgrade.gettext().tostring());             athlete.settier(integer.parseint(edittier.gettext().tostring()));              log.e("", "athlete name: " + athlete.getfirstname() + " " + athlete.getlastname());              athleteslist athleteslist = new athleteslist();             fragmenttransaction transaction = getfragmentmanager().begintransaction();             transaction.replace(r.id.framelayout, athleteslist, "athleteslist").commit();             // interface sends athlete main activity             communicator.send(athlete);         }     }); } 

your fragmenttag value different

set tag shows "athleteslist"

transaction.replace(r.id.framelayout, athleteslist, "athleteslist").commit();     

get tag shows "athletelist"

athleteslist athleteslist = (athleteslist) getsupportfragmentmanager().findfragmentbytag("athletelist"); 

athleteslist!=athletelist, therefore findfragmentbytag returns null.

for better solution, create public field constant can use tag fragment, such :

public static final string tag_fragment_athletelist = "tag_athletelist";

then use fragmenttag :

set

transaction.replace(r.id.framelayout, athleteslist, tag_fragment_athletelist).commit();   

get

athleteslist athleteslist = (athleteslist) getsupportfragmentmanager().findfragmentbytag(tag_fragment_athletelist); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

php - Find a regex to take part of Email -

javascript - Function overwritting -