android - NPE getCount in Adapter when trying to get user albums using FB SDK -
i trying users albums using fb sdk. have managed log-in , able retrieve users profile picture , acquire access token.
i following this answer here , trying work, running issue java.lang.nullpointerexception
in adapter class on at com.myapp.myapp.photosadapter.getcount(photosadapter.java:37)
here's full set-up have:
where going wrong? thanks
fbgrid activity:
public class fbgrid extends activity { private string url; // store paging url private string pagingurl; // flag current page int current_page = 1; // boolean check if new feeds loading boolean loadingmore = true; boolean stoploadingdata = false; gridview grid; arraylist<getphotos> arrphotos; photosadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fbgrid); grid = (gridview) findviewbyid(r.id.gridview1); grid.setonscrolllistener(new onscrolllistener() { @override public void onscrollstatechanged(abslistview view, int scrollstate) { } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { int lastinscreen = firstvisibleitem + visibleitemcount; if ((lastinscreen == totalitemcount) && !(loadingmore)) { if (stoploadingdata == false) { // fetch next batch of feeds new loadmorephotos().execute(); } } } }); new getphotosdata().execute(); } private class getphotosdata extends asynctask<void, void, void> { @override protected void doinbackground(void... arg0) { // change loading more status prevent duplicate calls // more data while loading batch loadingmore = true; session s = session.getactivesession(); // set initial url first lot of albums url = "https://graph.facebook.com/" + "me/albums" + "/photos&access_token=" + s.getaccesstoken() + "?limit=10"; try { httpclient hc = new defaulthttpclient(); httpget = new httpget(url); httpresponse rp = hc.execute(get); if (rp.getstatusline().getstatuscode() == httpstatus.sc_ok) { string queryalbums = entityutils.tostring(rp.getentity()); jsonobject jotemp = new jsonobject(queryalbums); jsonarray japhotos = jotemp.getjsonarray("data"); // in code, next page link here getphotos photos; (int = 0; < japhotos.length(); i++) { jsonobject jophotos = japhotos.getjsonobject(i); // log.e("individual albums", jophotos.tostring()); if (jophotos.has("link")) { photos = new getphotos(); // album id if (jophotos.has("id")) { photos.setphotoid(jophotos.getstring("id")); } else { photos.setphotoid(null); } // album name if (jophotos.has("name")) { photos.setphotoname(jophotos.getstring("name")); } else { photos.setphotoname(null); } // album cover photo if (jophotos.has("picture")) { photos.setphotopicture(jophotos .getstring("picture")); } else { photos.setphotopicture(null); } // photo's source if (jophotos.has("source")) { photos.setphotosource(jophotos .getstring("source")); } else { photos.setphotosource(null); } arrphotos.add(photos); } } } } catch (exception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { grid.setadapter(new photosadapter(fbgrid.this, arrphotos)); loadingmore = false; } } private class loadmorephotos extends asynctask<void, void, void> { @override protected void doinbackground(void... arg0) { // set loading more "true" loadingmore = true; // increment current page current_page += 1; // next page request url = pagingurl; try { httpclient hc = new defaulthttpclient(); httpget = new httpget(url); httpresponse rp = hc.execute(get); if (rp.getstatusline().getstatuscode() == httpstatus.sc_ok) { string queryalbums = entityutils.tostring(rp.getentity()); // log.e("paged result", queryalbums); jsonobject jotemp = new jsonobject(queryalbums); jsonarray japhotos = jotemp.getjsonarray("data"); // in code, next page link here getphotos photos; (int = 0; < japhotos.length(); i++) { jsonobject jophotos = japhotos.getjsonobject(i); // log.e("individual albums", jophotos.tostring()); if (jophotos.has("link")) { photos = new getphotos(); // album id if (jophotos.has("id")) { photos.setphotoid(jophotos.getstring("id")); } else { photos.setphotoid(null); } // album name if (jophotos.has("name")) { photos.setphotoname(jophotos.getstring("name")); } else { photos.setphotoname(null); } // album cover photo if (jophotos.has("picture")) { photos.setphotopicture(jophotos .getstring("picture")); } else { photos.setphotopicture(null); } // album's photo count if (jophotos.has("source")) { photos.setphotosource(jophotos .getstring("source")); } else { photos.setphotosource(null); } arrphotos.add(photos); } } } } catch (exception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { // listview current position - used maintain scroll position int currentposition = grid.getfirstvisibleposition(); // append new data arraylist , set adapter // listview adapter = new photosadapter(fbgrid.this, arrphotos); grid.setadapter(adapter); // setting new scroll position grid.setselection(currentposition + 1); // set loadingmore "false" after adding new feeds existing // list loadingmore = false; } } public class getphotos { string photoid; string photoname; string photopicture; string photosource; // set photo id public void setphotoid(string photoid) { this.photoid = photoid; } // photo id public string getphotoid() { return photoid; } // set photo name public void setphotoname(string photoname) { this.photoname = photoname; } // photo name public string getphotoname() { return photoname; } // set photo picture public void setphotopicture(string photopicture) { this.photopicture = photopicture; } // photo picture public string getphotopicture() { return photopicture; } // set photo source public void setphotosource(string photosource) { this.photosource = photosource; } // photo source public string getphotosource() { return photosource; } } }
and adapter class(it uses image loading library):
final class photosadapter extends baseadapter { private activity activity; arraylist<getphotos> arrayphotos; private static layoutinflater inflater = null; imageloader imageloader; public photosadapter(activity a, arraylist<getphotos> arrphotos) { activity = a; arrayphotos = arrphotos; inflater = (layoutinflater) activity .getsystemservice(context.layout_inflater_service); imageloader = new imageloader(activity.getapplicationcontext()); } public int getcount() { return arrayphotos.size(); } public object getitem(int position) { return arrayphotos.get(position); } public long getitemid(int position) { return position; } public view getview(final int position, view convertview, viewgroup parent) { viewholder holder; view vi = convertview; if (convertview == null) { vi = inflater.inflate(r.layout.photos_item, null); holder = new viewholder(); holder.imgphoto = (imageview) vi.findviewbyid(r.id.grid_item_image); vi.settag(holder); } else { holder = (viewholder) vi.gettag(); } if (arrayphotos.get(position).getphotopicture() != null) { imageloader.displayimage(arrayphotos.get(position) .getphotopicture(), holder.imgphoto); } return vi; } static class viewholder { imageview imgphoto; } }
here's logcat:
fatal exception: main java.lang.nullpointerexception @ com.myapp.myapp.photosadapter.getcount(photosadapter.java:37) @ android.widget.gridview.setadapter(gridview.java:186) @ com.myapp.myapp.fbgrid$getphotosdata.onpostexecute(fbgrid.java:173) @ com.myapp.myapp.fbgrid$getphotosdata.onpostexecute(fbgrid.java:1) @ android.os.asynctask.finish(asynctask.java:631) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:213) @ android.app.activitythread.main(activitythread.java:5225) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:741) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:557) @ dalvik.system.nativestart.main(native method)
Comments
Post a Comment