java - NullPointerException, can't get my textview from my layout -


edit: answered own question.

my textview null , don't understand why. use personal fragment manager maybe it's problem. id correct because if add fragment on main activity , change text fragment oncreate method change text function works. not work have fragmenta added activity, click on button , count number of time click on it. wanna change textview of fragmentc " counter equal .." , textview null. suspect reason null it's not yet added activity. oncreateview not called. way can fix ? should call fragment.oncreateview in respond function ?

my class textview:

public class fragmentc extends fragment {     textview textview;     @override     public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {         view view = inflater.inflate(r.layout.fragmentc, container,false);         //textview = (textview) view.findviewbyid(r.id.txtinfo2);         return view;     }      @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);         textview = (textview) getactivity().findviewbyid(r.id.txtinfo2);     }      public void changetext(string str){         if(textview==null)             log.e("truc", "null txt");         textview.settext("jghjh");     } } 

log gives txt null

fragmentc.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#854f98be">      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="fragment c"         android:id="@+id/txtinfo2"         android:layout_gravity="center_horizontal" /> </linearlayout> 

i put rest of code because have own fragmentmanager , may problem comes there:

my mainactivity:

public class mainactivity extends actionbaractivity implements communicator{      private monfragmentmanager monfragmentmanager;     public static final string tag="truc";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         monfragmentmanager = new monfragmentmanager                 (getsupportfragmentmanager(),r.id.container,new fragmenta(),new fragmentb(), new fragmentc());         monfragmentmanager.add(1, false);     }     @override public void respond(string data) {     fragmentc frag = (fragmentc) monfragmentmanager.getfragments()[2];     if(frag.getclass()==fragmentc.class)         log.e("truc","fragment correct");         //log gives "fragment correct".     frag.changetext(data); } } 

myownfragmentmanager class:

public class monfragmentmanager {     private fragmentmanager fm;       private fragment[] fragments;     int container;      public monfragmentmanager(fragmentmanager fm){         this.fm = fm;         fragments = fm.getfragments().toarray(new fragment[fm.getfragments().size()]);     }     public monfragmentmanager(fragmentmanager fm, int container,fragment...frags){         this.fm = fm;         this.container=container;         fragments = new fragment[frags.length];         for(int i=0;i<fragments.length;i++){             fragments[i]=frags[i];         }     }      public void hideallfragments(){...}      public void showfragment(int fragmentindex, boolean addtobackstack) {...}     public void add(int fragmentindex, boolean addtobackstack){         fragmenttransaction transaction = fm.begintransaction();          (int = 0; < fragments.length; i++) {             if (i == fragmentindex) {                 transaction.add(container,fragments[i]);             } else if(fragments[i].isadded()) {                 transaction.remove(fragments[i]);             }         }         if (addtobackstack) {             transaction.addtobackstack(null);         }         transaction.commit();     }     public void removeallfragments(){         fragmenttransaction transaction = fm.begintransaction();         for(int = 0; < fragments.length; i++) {             transaction.remove(fragments[i]);         }         transaction.commit();     }     public fragment[] getfragments() {         return fragments;     } } 

it safer findviewbyid() in onviewcreated method.

sample code:

@override public void onviewcreated(view view, bundle savedinstancestate) {     textview = (textview) view.findviewbyid(r.id.txtinfo2); 

notes:

  • with onviewcreated, the view parameter supplied, hint.
  • i realize you're using onactivitycreated method because triggered after oncreateview. believe upon experience, not documentation, view may not yet ready use. can test behavior rotating screen.

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -