java - Fragment Crashing whenbtrying to receive an object 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.
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 }
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); moverviewheading = (textview) getactivity().findviewbyid(r.id.frag_overview_heading_textview); } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.overview_list_item_fragment, container, false); } @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
you can't call findviewbyid in fragment oncreate(). there no ui @ time. try calling findviewbyid in oncreateview(). also, have use findviewbyid() on view inside fragment. can't use activity this.
like :
@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; }
Comments
Post a Comment