Display JSONArray to Custom ListView Exception Error Android AsyncTask Thread -
i working on getting jsonarray
server, , inserting custom listview. code executes, have no idea why program crashes before displayed. put system.out.println(x)
see how far console gets before crashes, runs through of them place?
thanks in advance help!
public class mainactivity extends activity { private listview list_view; private trendingadapter ta = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //create listview, defined in activity_main.xml file this.list_view = (listview) findviewbyid(r.id.list_view); system.out.println("1"); //display trending results try { get_trending(); system.out.println("16"); } catch (exception e) { e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } //async class... private class asyncop extends asynctask<string, void, string> { @override protected string doinbackground(string... strings) { system.out.println("5"); jsonobject response_json = new jsonobject(); jsonobject request_json = new jsonobject(); //take passed string[0] , turn json try { request_json = new jsonobject(strings[0]); } catch (exception e) { e.printstacktrace(); } //send json server , grab response try { system.out.println("6"); httpclient client = new defaulthttpclient(); string request_url = "http://some_ip/api/api.php?package=" + uri.encode(request_json.tostring(), null); httpget get_request = new httpget(request_url); httpresponse get_response = client.execute(get_request); httpentity get_entity = get_response.getentity(); system.out.println("7"); //ensure response before doing foolish if (get_entity != null) { system.out.println("8"); string response = entityutils.tostring(get_entity); response_json = new jsonobject(response); return response_json.tostring(); } } catch (exception e) { e.printstacktrace(); } //if we're here, above response didn't work reason... return "crazy shit happened"; } //what after request done... @override protected void onpostexecute(string result) { system.out.println("9"); //call resppnse parser function... try { server_response(result); system.out.println("14"); } catch (exception e) { e.printstacktrace(); } } } private class trendingadapter extends arrayadapter<trendingtopic> { private context context; private int resource_id; private arraylist<trendingtopic> trending_list = new arraylist<trendingtopic>(); public trendingadapter(context context, int resource_id, arraylist<trendingtopic> trending_list) { super(context, resource_id, trending_list); this.resource_id = resource_id; this.context = context; this.trending_list = trending_list; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = new viewholder(convertview); holder = (viewholder) convertview.gettag(); holder.populatefrom(trending_list.get(position)); return (convertview); } } private class viewholder { public textview topic_rank = null; public textview topic_title = null; public textview num_comments = null; public viewholder(view row) { this.topic_rank = (textview) row.findviewbyid(r.id.topic_rank); this.topic_title = (textview) row.findviewbyid(r.id.topic_title); this.num_comments = (textview) row.findviewbyid(r.id.num_comments); } public void populatefrom(trendingtopic topic) { this.topic_rank.settext(topic.rank); this.topic_title.settext(topic.body.substring(0, math.min(topic.body.length(), 30))); this.num_comments.settext(topic.comments); } } public void get_trending() throws jsonexception { system.out.println("2"); jsonobject type = new jsonobject(); jsonobject request = new jsonobject(); type.put("type", "trending"); request.put("key", "some_key"); request.put("request", "info"); request.put("info", type); system.out.println("3"); asyncop operation = new asyncop(); system.out.println("4"); operation.execute(new string[] {request.tostring()}); system.out.println("15"); } public void server_response(string json_string) throws jsonexception { system.out.println("10"); //get json info arraylist jsonobject json = new jsonobject(json_string); jsonarray trending = json.getjsonarray("trending"); arraylist<trendingtopic> trending_list = new arraylist<trendingtopic>(); int x = 0; int end_of_list = trending.length(); trendingtopic topic = new trendingtopic(); while (x < end_of_list) { topic = new trendingtopic(trending.getjsonobject(x)); trending_list.add(topic); x++; } system.out.println("11"); //display response main listview this.ta = new trendingadapter(this, android.r.layout.simple_list_item_1, trending_list); system.out.println("12"); this.list_view.setadapter(ta); system.out.println("13"); } }
and produced in logcat
05-14 03:11:16.410: i/system.out(1706): 1 05-14 03:11:16.410: i/system.out(1706): 2 05-14 03:11:16.410: i/system.out(1706): 3 05-14 03:11:16.410: i/system.out(1706): 4 05-14 03:11:16.420: i/system.out(1706): 15 05-14 03:11:16.420: i/system.out(1706): 16 05-14 03:11:16.430: i/system.out(1706): 5 05-14 03:11:16.430: i/system.out(1706): 6 05-14 03:11:16.561: d/gralloc_goldfish(1706): emulator without gpu emulation detected. 05-14 03:11:17.771: i/system.out(1706): 7 05-14 03:11:17.771: i/system.out(1706): 8 05-14 03:11:17.840: d/dalvikvm(1706): gc_concurrent freed 844k, 24% free 3177k/4164k, paused 5ms+9ms, total 36ms 05-14 03:11:18.320: i/system.out(1706): 9 05-14 03:11:18.320: i/system.out(1706): 10 05-14 03:11:18.470: d/dalvikvm(1706): gc_concurrent freed 370k, 22% free 3278k/4164k, paused 12ms+21ms, total 101ms 05-14 03:11:18.491: i/system.out(1706): 11 05-14 03:11:18.500: i/system.out(1706): 12 05-14 03:11:18.500: i/system.out(1706): 13 05-14 03:11:18.500: i/system.out(1706): 14 05-14 03:11:18.510: d/androidruntime(1706): shutting down vm 05-14 03:11:18.510: w/dalvikvm(1706): threadid=1: thread exiting uncaught exception (group=0x40a71930) 05-14 03:11:18.542: e/androidruntime(1706): fatal exception: main 05-14 03:11:18.542: e/androidruntime(1706): java.lang.nullpointerexception 05-14 03:11:18.542: e/androidruntime(1706): @ com.example.actionbartutorial.mainactivity$viewholder.<init>(mainactivity.java:160) 05-14 03:11:18.542: e/androidruntime(1706): @ com.example.actionbartutorial.mainactivity$trendingadapter.getview(mainactivity.java:144) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.abslistview.obtainview(abslistview.java:2159) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.listview.measureheightofchildren(listview.java:1246) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.listview.onmeasure(listview.java:1158) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.view.measure(view.java:15518) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.relativelayout.measurechild(relativelayout.java:666) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.relativelayout.onmeasure(relativelayout.java:477) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.view.measure(view.java:15518) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.framelayout.onmeasure(framelayout.java:310) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.view.measure(view.java:15518) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.linearlayout.measurevertical(linearlayout.java:847) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.view.measure(view.java:15518) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) 05-14 03:11:18.542: e/androidruntime(1706): @ android.widget.framelayout.onmeasure(framelayout.java:310) 05-14 03:11:18.542: e/androidruntime(1706): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2176) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.view.measure(view.java:15518) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1874) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1089) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1265) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:989) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4351) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.choreographer$callbackrecord.run(choreographer.java:749) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.choreographer.docallbacks(choreographer.java:562) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.choreographer.doframe(choreographer.java:532) 05-14 03:11:18.542: e/androidruntime(1706): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:735) 05-14 03:11:18.542: e/androidruntime(1706): @ android.os.handler.handlecallback(handler.java:725) 05-14 03:11:18.542: e/androidruntime(1706): @ android.os.handler.dispatchmessage(handler.java:92) 05-14 03:11:18.542: e/androidruntime(1706): @ android.os.looper.loop(looper.java:137) 05-14 03:11:18.542: e/androidruntime(1706): @ android.app.activitythread.main(activitythread.java:5041) 05-14 03:11:18.542: e/androidruntime(1706): @ java.lang.reflect.method.invokenative(native method) 05-14 03:11:18.542: e/androidruntime(1706): @ java.lang.reflect.method.invoke(method.java:511) 05-14 03:11:18.542: e/androidruntime(1706): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-14 03:11:18.542: e/androidruntime(1706): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-14 03:11:18.542: e/androidruntime(1706): @ dalvik.system.nativestart.main(native method)
you haven't accounted fact convertview
valid object once views start getting recycled. initially, when first views created list, convertview
null, , viewholder
constructor doesn't handle null parameter gracefully.
your viewholder
pattern bit broken. when convertview
null, have inflate , return new instance of view used, , in instance create new holder. should have more like:
viewholder holder; if (convertview == null) { convertview = getlayoutinflater().inflate(resource_id, parent, false); holder = new viewholder(convertview); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.populatefrom(trending_list.get(position)); return convertview;
Comments
Post a Comment