How to fix null pointer exception when using two Volley requests in same activity in Android? -
i have added "featured" pager view header listview. pager view receives data separate url listview making 2 separate volley requests. i've tested pager view in separate project , works flawlessly. listview works flawlessly without additional volley request well. main activity.
public class mainactivity extends appcompatactivity { private progressdialog dialog=null ; private customadapter adapter; private listview list; arraylist<rowdata> rowdata; private searchview searchview; private menuitem myactionmenuitem; private viewpager pageview; private arraylist<featuremodel> fdata; private featureadapter featadapt; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); layoutinflater inflater = getlayoutinflater(); viewgroup header = (viewgroup) inflater.inflate(r.layout.feature_view, list, false); pageview = (viewpager)findviewbyid(r.id.featuredview); list=(listview) findviewbyid(r.id.listview1); list.addheaderview(header); list.settextfilterenabled(true); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { //get item @ position rowdata item = (rowdata)parent.getitematposition(position); //pass image title , url detailsactivity intent intent = new intent(mainactivity.this, detailactivity.class); intent.putextra("title", item.gettitle()); intent.putextra("image", item.getimageurl()); intent.putextra("size", item.getprodsize()); intent.putextra("date", item.getproddate()); intent.putextra("region", item.getregionurl()); intent.putextra("desc", item.getdetailurl()); //start details activity startactivity(intent); } }); rowdata=new arraylist<rowdata>(); dialog= new progressdialog(this, r.style.apptheme); dialog.setmessage("loading..."); dialog.show(); jsonarrayrequest request = new jsonarrayrequest(url+url_file, new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { log.d(tag, response.tostring()); try { for(int i=0;i<response.length();i++){ string title=response.getjsonobject(i).getstring("prod_name"); string size=response.getjsonobject(i).getstring("prod_size"); string img_url; string date = response.getjsonobject(i).getstring("prod_date"); string detail_url = response.getjsonobject(i).getstring("prod_desc"); string region_url = response.getjsonobject(i).getstring("prod_region"); img_url = response.getjsonobject(i).getstring("prod_image"); rowdata.add(new rowdata(title, size, img_url,detail_url,region_url,date)); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } adapter=new customadapter(mainactivity.this, rowdata); list.setadapter(adapter); dialog.dismiss(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d(tag, "error: " + error.getmessage()); dialog.dismiss(); } }); volleycontroller.getinstance().addtorequestqueue(request, tag_json_arry); loadfeatures(); } public void loadfeatures(){ jsonarrayrequest stringrequest = new jsonarrayrequest(feat_url, new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { log.d(tag, response.tostring()); try { for(int i=0;i<response.length();i++){ string sid=response.getjsonobject(i).getstring("fid"); string name=response.getjsonobject(i).getstring("feat_name"); string img = response.getjsonobject(i).getstring("feat_pic"); fdata.add(new featuremodel(sid, name, img)); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } featadapt=new featureadapter(mainactivity.this, fdata); pageview.setadapter(featadapt); //dialog.dismiss(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d(tag, "error: " + error.getmessage()); //dialog.dismiss(); } }); volleycontroller.getinstance().addtorequestqueue(stringrequest, tag); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); myactionmenuitem = menu.finditem(r.id.action_search); searchview = (searchview) myactionmenuitem.getactionview(); searchview.clearfocus(); searchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string query) { return false; } @override public boolean onquerytextchange(string newtext) { filter filter = adapter.getfilter(); if (textutils.isempty(newtext)) { filter.filter(""); } else { filter.filter(newtext); } return true; } }); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_search) { return true; } if(id == r.id.action_info){ return true; } if(id == r.id.action_share){ return true; } return super.onoptionsitemselected(item); } @override protected void onresume() { super.onresume(); } @override protected void onpause() { super.onpause(); } }
and logcat:
process: com.curtrostudios.prodarchive, pid: 7231 java.lang.nullpointerexception @ com.curtrostudios.prodarchive.mainactivity$4.onresponse(mainactivity.java:149) @ com.curtrostudios.prodarchive.mainactivity$4.onresponse(mainactivity.java:140) @ com.android.volley.toolbox.jsonrequest.deliverresponse(jsonrequest.java:65) @ com.android.volley.executordelivery$responsedeliveryrunnable.run(executordelivery.java:99) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5072) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:602) @ dalvik.system.nativestart.main(native method)
after continuous research , trial , error, figured out issue was. problem not having 2 volley requests rather volley request returning json response null view.
initially pagerview looked this:
pageview = (viewpager)findviewbyid(r.id.featuredview);
for wants include viewpager header listview, how should done.
the header view declared so:
view header = (relativelayout)getlayoutinflater().inflate(r.layout.feature_view, null);
so if want use view within layout, have declare
pageview = (viewpager)header.findviewbyid(r.id.featuredview);
header.findviewbyid()
ensures finding view within declared header.
then ensure header showing correctly, had add following:
final displaymetrics displaymetrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); final int width = displaymetrics.widthpixels; abslistview.layoutparams headerviewparams = new abslistview.layoutparams(width, 400); header.setlayoutparams(headerviewparams);
this complete code looks in activity's oncreate() method:
//use find width of device final displaymetrics displaymetrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); //this declare header layout view header = (relativelayout)getlayoutinflater().inflate(r.layout.feature_view, null); //this find view within header layout //that you're going need or use pageview = (viewpager)header.findviewbyid(r.id.featuredview); //this set layout params if header layout //isn't showing abslistview.layoutparams headerviewparams = new abslistview.layoutparams(width, 400); //set layout params header header.setlayoutparams(headerviewparams); list=(listview)findviewbyid(r.id.listview1); //add header listview. //in case, listview = list; list.addheaderview(header);
Comments
Post a Comment