android - Custom adapter for Listview from AsyncTask -
i trying list of crops mysql database on server , populate listview list. below code:
this asynctask class:
class getcropslist extends asynctask<string, integer, arraylist<string>> { private final progressdialog dialog = new progressdialog(cropslistactivity.this); @override protected void onpreexecute() { dialog.setmessage("please wait."); dialog.setcancelable(true); dialog.show(); } @override protected arraylist<string> doinbackground(string... params) { arraylist<string> list = null; string lang=params[0]; log.i("lang",lang); seek { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://10.0.2.2/farm_o_pedia/getcropslist.php"); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(1); namevaluepairs.add(new basicnamevaluepair("languagename",lang)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse res= httpclient.execute(httppost); log.i("http ok", res.tostring()); httpentity entity = res.getentity(); input=entity.getcontent(); } grab (exception e) { log.i("http failed", e.tostring()); } try{ bufferedreader reader = new bufferedreader(new inputstreamreader(input,"utf-8")); sb = new stringbuilder(); sb.append(reader.readline() + "\n"); string line="0"; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } input.close(); result=sb.tostring(); log.i("jsonobj",result); } catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } seek { jarray=new jsonarray(result); jsonobject obj=null; for(int i=0;i<jarray.length();i++) { obj=jarray.getjsonobject(i); list.add(obj.getstring("crop_name")); } } grab (jsonexception e) { e.printstacktrace(); } homecoming list; } protected void onpostexecute(final arraylist<string> p_result) { super.onpostexecute(p_result); if (dialog.isshowing()) { dialog.dismiss(); } arrayadapter<string> adapter = new arrayadapter<string>(cropslistactivity.this, r.layout.crops_listview,p_result ) { public view getview(int position, view convertview, viewgroup parent) { view v = super.getview(position, convertview, parent); ((textview) v).settypeface(tf); ((textview) v).settext(p_result.get(position)); ((textview) v).settextcolor(color.white); homecoming v; } }; cropslist.setadapter(adapter); } }
i have taken care of not touching ui in dobackground. logcat is:
02-18 11:20:44.301: e/androidruntime(597): fatal exception: asynctask #2 02-18 11:20:44.301: e/androidruntime(597): java.lang.runtimeexception: error occured while executing doinbackground() 02-18 11:20:44.301: e/androidruntime(597): @ android.os.asynctask$3.done(asynctask.java:200) 02-18 11:20:44.301: e/androidruntime(597): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:274) 02-18 11:20:44.301: e/androidruntime(597): @ java.util.concurrent.futuretask.setexception(futuretask.java:125) 02-18 11:20:44.301: e/androidruntime(597): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:308) 02-18 11:20:44.301: e/androidruntime(597): @ java.util.concurrent.futuretask.run(futuretask.java:138) 02-18 11:20:44.301: e/androidruntime(597): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 02-18 11:20:44.301: e/androidruntime(597): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 02-18 11:20:44.301: e/androidruntime(597): @ java.lang.thread.run(thread.java:1019) 02-18 11:20:44.301: e/androidruntime(597): caused by: java.lang.nullpointerexception 02-18 11:20:44.301: e/androidruntime(597): @ com.example.farm_o_pedia.cropslistactivity$getcropslist.doinbackground(cropslistactivity.java:170) 02-18 11:20:44.301: e/androidruntime(597): @ com.example.farm_o_pedia.cropslistactivity$getcropslist.doinbackground(cropslistactivity.java:1) 02-18 11:20:44.301: e/androidruntime(597): @ android.os.asynctask$2.call(asynctask.java:185) 02-18 11:20:44.301: e/androidruntime(597): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 02-18 11:20:44.301: e/androidruntime(597): ... 4 more 02-18 11:20:45.660: e/windowmanager(597): activity com.example.farm_o_pedia.cropslistactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@40529ed8 added here 02-18 11:20:45.660: e/windowmanager(597): android.view.windowleaked: activity com.example.farm_o_pedia.cropslistactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@40529ed8 added here 02-18 11:20:45.660: e/windowmanager(597): @ android.view.viewroot.<init>(viewroot.java:258) 02-18 11:20:45.660: e/windowmanager(597): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:148) 02-18 11:20:45.660: e/windowmanager(597): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:91) 02-18 11:20:45.660: e/windowmanager(597): @ android.view.window$localwindowmanager.addview(window.java:424) 02-18 11:20:45.660: e/windowmanager(597): @ android.app.dialog.show(dialog.java:241) 02-18 11:20:45.660: e/windowmanager(597): @ com.example.farm_o_pedia.cropslistactivity$getcropslist.onpreexecute(cropslistactivity.java:116) 02-18 11:20:45.660: e/windowmanager(597): @ android.os.asynctask.execute(asynctask.java:391) 02-18 11:20:45.660: e/windowmanager(597): @ com.example.farm_o_pedia.cropslistactivity.oncreate(cropslistactivity.java:80) 02-18 11:20:45.660: e/windowmanager(597): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 02-18 11:20:45.660: e/windowmanager(597): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 02-18 11:20:45.660: e/windowmanager(597): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 02-18 11:20:45.660: e/windowmanager(597): @ android.app.activitythread.access$1500(activitythread.java:117) 02-18 11:20:45.660: e/windowmanager(597): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 02-18 11:20:45.660: e/windowmanager(597): @ android.os.handler.dispatchmessage(handler.java:99) 02-18 11:20:45.660: e/windowmanager(597): @ android.os.looper.loop(looper.java:123) 02-18 11:20:45.660: e/windowmanager(597): @ android.app.activitythread.main(activitythread.java:3683) 02-18 11:20:45.660: e/windowmanager(597): @ java.lang.reflect.method.invokenative(native method) 02-18 11:20:45.660: e/windowmanager(597): @ java.lang.reflect.method.invoke(method.java:507) 02-18 11:20:45.660: e/windowmanager(597): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 02-18 11:20:45.660: e/windowmanager(597): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 02-18 11:20:45.660: e/windowmanager(597): @ dalvik.system.nativestart.main(native method)
any help appreciable.
in doingbackground()
just replace
arraylist<string> list = null;
with
arraylist<string> list = new arraylist<string>();
android android-listview android-asynctask
No comments:
Post a Comment