android - View is null in Fragment -


i have number of fragments within viewpager in 1 of activities. these fragments have text on them. goal update text on fragments when event happens in activity.

i have tried things, cannot seem find view work with. in below code have tried save view oncreateview private field. works when creating fragment, when try , call refresh mview null.

refresh implemented in interface. viewpager has 2 different fragments, implemented interface allow me cast unknown fragment call refresh.

i have tried write below code best illustrate problem. can set text "hello" on refresh, activity crash because mview null.

    private view mview = null;      @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         if (container == null) { return null; }                  view view = inflater.inflate(r.layout.view_account_general, container, false);         mview = view;         setcontent();         return view;     }      private void setcontent() {         if (mview == null) return;         ((textview) mview.findviewbyid(r.id.mid)).settext("hello");     }      @override     public void refresh() {         ((textview) mview.findviewbyid(r.id.mid)).settext("test");     } 

what doing wrong , should doing instead? fragments new me , haven't programmed android in long time. intention update value of textview when refresh method called.

thank attempts @ helping me issue!

edit: requested, exception trace.

fatal exception: main java.lang.nullpointerexception     @ com.jordanmoore.finances.accountgeneralfragment.refresh(accountgeneralfragment.java:85)     @ com.jordanmoore.finances.accountactivity.onoptionsitemselected(accountactivity.java:222)     @ android.app.activity.onmenuitemselected(activity.java:2534)     @ android.support.v4.app.fragmentactivity.onmenuitemselected(fragmentactivity.java:361)     @ com.android.internal.policy.impl.phonewindow.onmenuitemselected(phonewindow.java:964)     @ com.android.internal.view.menu.menubuilder.dispatchmenuitemselected(menubuilder.java:735)     @ com.android.internal.view.menu.menuitemimpl.invoke(menuitemimpl.java:149)     @ com.android.internal.view.menu.menubuilder.performitemaction(menubuilder.java:874)     @ com.android.internal.view.menu.menupopuphelper.onitemclick(menupopuphelper.java:156)     @ android.widget.adapterview.performitemclick(adapterview.java:298)     @ android.widget.abslistview.performitemclick(abslistview.java:1090)     @ android.widget.abslistview$performclick.run(abslistview.java:2863)     @ android.widget.abslistview$1.run(abslistview.java:3538)     @ android.os.handler.handlecallback(handler.java:615)     @ android.os.handler.dispatchmessage(handler.java:92)     @ android.os.looper.loop(looper.java:213)     @ android.app.activitythread.main(activitythread.java:4786)     @ java.lang.reflect.method.invokenative(native method)     @ java.lang.reflect.method.invoke(method.java:511)     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:789)     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:556)     @ dalvik.system.nativestart.main(native method) 

bottom line- fragment should in control of refreshing it's own view. activity should never call "refresh" method in fragment, because it's painful , error-prone determine when fragment initialized , ready render.

in case, activity broadcast "update", fragments register receive. long ensure register after preconditions have been met, , unregister before preconditions become invalid, things work. e.g., follow typical pattern of registering in onresume() on unregistering in onpause().


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -