java - Fragment Crashing when receiving data from an Activity -
good day all,
i have issue activity making network call , when network call completed, makes changes in activity using data json object received call, passes object down fragments in same activity. these fragments in tablayout.
i had same issue asked here at question sorted out seem having same issue, after worked little bit after not changing significant. adding more fields wanted change?
the issue have if put system.out.println() prints out correct data. minute want set textview data receive in fragment app crashes nullpointer. when debug debug in android studio, textview i'm setting null reason.
activity code initial network call:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_listings); butterknife.bind(this); setsupportactionbar(toolbar); handleintent(getintent()); } private void handleintent(intent aintent) { if (aintent != null) { string tradetype = aintent.getstringextra("itemtype"); string tradeid = aintent.getstringextra("itemid"); presenter = new itempresenterimpl(this, itembuynowactivity.this); presenter.dolistingservicecall(tradeid); // <------- send trade id can network call. } else { system.out.println("intent null in " + itembuynowactivity.class.getsimplename()); } }
interface between activity , presenter:
public interface itempresenter { void dolistingservicecall(string itemid); //<------- comes interface void dotoolbarbackgroundimagecall(tradeitem atradeitem); }
class implements presenter:
@override public void dolistingservicecall(string aitemid) { // <------- network call starts string homeurl = bobeapplication.getinstance().getwsurl() + mcontext.getstring(r.string.ws_url_item) + aitemid; bobejsonrequest jsobjrequest = new bobejsonrequest(request.method.get, homeurl, null, this, this); volleysingleton.getinstance().addtorequestqueue(jsobjrequest, "listingrequest"); } @override public void dotoolbarbackgroundimagecall(tradeitem atradeitem) { imagerequest request = new imagerequest(atradeitem.getitem().getimageurl(), new response.listener<bitmap>() { @override public void onresponse(bitmap bitmap) { drawable drawable = new bitmapdrawable(mcontext.getresources(), bitmap); mitemview.loadbackgroundimage(drawable); } }, 0, 0, null, new response.errorlistener() { public void onerrorresponse(volleyerror error) { mitemview.displayerrormessage(volleyerrorhelper.geterrortype(error, mcontext) + " occurred downloading background image"); } }); volleysingleton.getinstance().addtorequestqueue(request, "listitemtoolbarbackgroundimagerequest"); } @override public void onresponse(object response) { gson gson = new gson(); tradeitem tradeitem = gson.fromjson(response.tostring(), tradeitem.class); mitemview.populatelistviews(tradeitem); // <------- send object views in activity can manipulated dotoolbarbackgroundimagecall(tradeitem); }
method in activity handles
@override public void populatelistviews(tradeitem atradeitem) { moverviewpresenter = new overviewpresenterimpl(new overviewlistitemfragment(), atradeitem); overviewlistitemfragment.setdata(atradeitem); //<------- send object fragment can manipulate views in fragment } class tabadapter extends fragmentpageradapter { public tabadapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { fragment fragment = null; if (position == 0) { fragment = new overviewlistitemfragment(); } if (position == 1) { fragment = new descriptionlistitemfragment(); } if (position == 2) { fragment = new shippinglistitemfragment(); } if (position == 3) { fragment = new paymentlistitemfragment(); } return fragment; } @override public int getcount() { return 4; } @override public charsequence getpagetitle(int position) { if (position == 0) { return "overview"; } if (position == 1) { return "description"; } if (position == 2) { return "shipping"; } if (position == 3) { return "payment"; } return null; } }
the fragment receives data:
public class overviewlistitemfragment extends fragment implements overviewview { private static textview moverviewheading; public overviewlistitemfragment() { } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.overview_list_item_fragment, container, false); moverviewheading = (textview) view.findviewbyid(r.id.frag_overview_heading_textview); return view; } @override public void populateoverviewviews(final tradeitem atradeitem) { system.out.println("overview trade object title is:" + atradeitem.getitem().gettradetitle()); // <------- print statement works 100% when try setting moverviewheading text in atradeitem.getitem().gettradetitle() null pointer exception. } public static void setdata(tradeitem atradeitem) { system.out.println("overview trade object title is:" + atradeitem.getitem().gettradetitle()); // <------- print statement works 100% when try setting moverviewheading text in atradeitem.getitem().gettradetitle() null pointer exception. moverviewheading.settext(atradeitem.getitem().gettradetitle());// <------- crashes , moverviewheading still null @ point. } }
edit: sorry forgot logcat:
02-05 17:08:21.554 30512-30512/com.example.app e/androidruntime: fatal exception: main java.lang.nullpointerexception @ com.example.app.ui.fragments.overviewlistitemfragment.setdata(overviewlistitemfragment.java:46) @ com.example.app.ui.activities.itembuynowactivity.populatelistviews(itembuynowactivity.java:95) @ com.example.app.listing.itempresenterimpl.onresponse(itempresenterimpl.java:62) @ com.android.volley.toolbox.jsonrequest.deliverresponse(jsonrequest.java:65) @ com.android.volley.executordelivery$responsedeliveryrunnable.run(executordelivery.java:99) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5041) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) @ dalvik.system.nativestart.main(native method)
my thinking view i'm trying set isn't "active" (if thats right word) @ time receives data, because when run debugger break point @ method receives data in fragment, moverviewheading textview id null, though have findviewbyid in oncreate, tried placing in oncreateview() both times failed. tried placing findviewbyid in same method gets called when response successful before try setting settext() on textview.
thank you
overviewlistitemfragment
assume not added fragment instance, class.
i suggest following changes: remove static
setdata
, textview
, leave it, if know how works. don't think necessary or recommendable.
private overviewlistitemfragment mfrag; //declare globally mfrag = new overviewlistitemfragment(); //if not want add now, ignore following line getsupportfragmentmanager().begintransaction().add(r.id.yourcontainer, mfrag, "mfrag").commit();
now call mfrag.setdata
everytime want set data. check if mfrag
null, reinitialize, , maybe re-add, or whatever want do.
edit: know use viewpager
, suggest following:
do above. don't think recommendable have static methods in context
. error because trying reach textview
in fragment
. initialized in viewpager
/pageradapter
, , pageradapter
holds reference used instance of fragment. can access used fragment through
fragment mfragment = pageradapter.getfragment(0); //frag @ position 0
with casting, able find (now not static) method:
((overviewlistitemfragment)pageradapter.getfragment(0)).setdata(your_data);
please add try/catch
. check if fragment null
, because possible fragment recycled in fragmentpageradapter
, because reached offset. way achieve this, store required data, , update everytime fragment gets visible described here.
edit 2: obviously, you'll need changed in adapter
:
i recommend creating array containing fragment in constructor:
//global in adapter: private fragment[] fragments; public custompageradapter(fragmentmanager fm) { super(fm); fragments = new gamefragment[4]; fragments[0] = new myfragment(); fragments[1] = new secondfragment(); .... } public fragment getitem(int position) { return fragments[position]; } public fragment getfragment(int position) { return fragments[position]; }
Comments
Post a Comment