android - can't access view inside fragment method. it returns null object -


i'm building app based on tabbedactivity preset of android studio ,therefore fragments created so:

private sectionspageradapter msectionspageradapter;      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); } 

and later on:

public class sectionspageradapter extends fragmentpageradapter {          public sectionspageradapter(fragmentmanager fm) {             super(fm);         }          @override         public fragment getitem(int position) {             switch (position) {                 case 0:                     dashboardtab tabdashboard = new dashboardtab();                     return tabdashboard;                 case 1:                     vertretungsplantab tabvertretungsplan = new vertretungsplantab();                     return tabvertretungsplan;                 default:                     return null;             }         }          @override         public int getcount() {             // show 2 total pages.             return 2;         }          @override         public charsequence getpagetitle(int position) {             switch (position) {                 case 0:                     return "dashboard";                 case 1:                     return "vertretungsplan";             }             return null;         }     } 

this working fine in fact quite uncommon way of creating tabs might problem caused missunderstanding of mainactivity.java

public class vertretungsplantab extends fragment {     @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.vertretungsplan_fragment, container, false);         return rootview;     }        public void settext(string texttobeset, vertretungsplantab tab){         textview tv = (textview) getview().findviewbyid(r.id.textview2);     } 

now problem far caused view, because null object inside method.

attempt invoke virtual method 'android.view.view android.view.view.findviewbyid(int)' on null object reference 

i've tried several ways of accessing view, includes creating new variable in fragment , commiting view in oncreateview(). if checked in oncreatedview() wasn't yet null object.

i've tried create textview in oncreateview(), after oncreatedview() isn't possible call settext()

after searching internet several days great if more advanced developer solution!

okay not clear question wanna call settext() method. better if you've added place want call settext().

but far understand situation, must've calling method outside of fragment , must creating object of fragment. if call settext() method inside fragment in onviewcreated() 100% work if try calling method this:

vertretungsplantab tab = new vertretungsplantab(); tab.settext("any text", tab); 

it return nullpointerexception because you've created new object of fragment , know object blueprint of class means doesn't have values after modification it.

i've used fragments lot , have faces these situations many times. if have class want's update text inside fragment, handlers best choice have.

broadcastreceivers not bad choice such situations , can use interfaces or abstract classes this. try learn these concepts because these best thing have when want pass data 1 class another.

an example using interfaces here. example, it's not similar problem through idea of using interfaces. go through it..i hope help.

you can learn how use handlers pass data activity fragment or fragment activity.

if i'm getting question wrong please explain in more detail because can understand question :)


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -