android - NullPointerException when setAdapter ListView in onPostExcute -
i create listview
, set action loadmore when scrolling final item. listview
works until scroll final item, action loadmore listview
running. got error:
07-01 22:48:10.792: e/androidruntime(4691): java.lang.nullpointerexception 07-01 22:48:10.792: e/androidruntime(4691): @ android.widget.listview.clearrecycledstate(listview.java:516) 07-01 22:48:10.792: e/androidruntime(4691): @ android.widget.listview.resetlist(listview.java:503) 07-01 22:48:10.792: e/androidruntime(4691): @ android.widget.listview.setadapter(listview.java:445) 07-01 22:48:10.792: e/androidruntime(4691): @ com.example.mainactivity$1processdatatask.onpostexecute(mainactivity.java:192) 07-01 22:48:10.792: e/androidruntime(4691): @ com.example.mainactivity$1processdatatask.onpostexecute(mainactivity.java:1) 07-01 22:48:10.792: e/androidruntime(4691): @ android.os.asynctask.finish(asynctask.java:631) 07-01 22:48:10.792: e/androidruntime(4691): @ android.os.asynctask.access$600(asynctask.java:177) 07-01 22:48:10.792: e/androidruntime(4691): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) 07-01 22:48:10.792: e/androidruntime(4691): @ android.os.handler.dispatchmessage(handler.java:99) 07-01 22:48:10.792: e/androidruntime(4691): @ android.os.looper.loop(looper.java:137) 07-01 22:48:10.792: e/androidruntime(4691): @ android.app.activitythread.main(activitythread.java:5103) 07-01 22:48:10.792: e/androidruntime(4691): @ java.lang.reflect.method.invokenative(native method) 07-01 22:48:10.792: e/androidruntime(4691): @ java.lang.reflect.method.invoke(method.java:525) 07-01 22:48:10.792: e/androidruntime(4691): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 07-01 22:48:10.792: e/androidruntime(4691): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 07-01 22:48:10.792: e/androidruntime(4691): @ dalvik.system.nativestart.main(native method)
nullpointexception
got in listview.setadapter(adapter);
in onpostexcute
method, in asynctask
.
this class:
public class mainactivity extends actionbaractivity { list<person> listdata; listview listview; customadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.person_layout); listview = (listview) findviewbyid(r.id.list_view_person); listview.setdivider(null); listdata = new arraylist<person>(); processdata(); listview.setonscrolllistener(new onscrolllistener() { @override public void onscrollstatechanged(abslistview view, int scrollstate) { } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { if(totalitemcount > 0 && firstvisibleitem + visibleitemcount == totalitemcount) { processdata(); } } }); } public void processdata(){ class processdatatask extends asynctask<void, void, string> { public processdatatask() { super(); } @override protected string doinbackground(void... params) { .........//add data listdata here return null; } @override protected void onpostexecute(string result) { if(listdata.size() > 0){ adapter = new customadapter(listdata, mainactivity.this); if(listview != null && adapter != null) listview.setadapter(adapter); } super.onpostexecute(result); } } processdatatask processdata = new processdatatask(); processdata.execute(); } }
is there suggestion solve error?
you forget arraylist
initialization... add code
listdata = new arraylist<person>(); processdata();
Comments
Post a Comment