Adding an Fragment id to auto-generated fragment from android studio -
i have created auto generated fragment android studio.
when
tagdatafragment fragment = (tagdatafragment) getsupportfragmentmanager().findfragmentbyid(r.id.tagfragment); fragment.fillvalues();
fragment
returns null
. have googled , found id not added fragment. how , add fragment id .
import android.support.design.widget.tablayout; import android.support.v4.app.fragmenttransaction; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.edittext; import android.widget.textview; public class mainactivity extends appcompatactivity { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link fragmentpageradapter} derivative, keep every * loaded fragment in memory. if becomes memory intensive, * may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ private sectionspageradapter msectionspageradapter; /** * {@link viewpager} host section contents. */ private viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); // create adapter return fragment each of 3 // primary sections of activity. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { tagdatafragment fragment = (tagdatafragment) getsupportfragmentmanager().findfragmentbyid(r.id.tagfragment); fragment.fillvalues(); return true; } return super.onoptionsitemselected(item); } public void fillvalues(menuitem item) { } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; public placeholderfragment() { } /** * returns new instance of fragment given section * number. */ public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); textview textview = (textview) rootview.findviewbyid(r.id.section_label); textview.settext(getstring(r.string.section_format, getarguments().getint(arg_section_number))); return rootview; } } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int index) { // getitem called instantiate fragment given page. // return placeholderfragment (defined static inner class below). switch (index) { case 0: // top rated fragment activity return new tagdatafragment(); case 1: // games fragment activity return new tagdatafragment(); } return null; } @override public int getcount() { // show 3 total pages. return 2; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "section 1"; case 1: return "section 2"; } return null; } } }
Comments
Post a Comment