android - Application crashes when scrolling listview up or down if listAdapter is notified while scrolling and crashes after about 20 seconds without scrolling -
i have listview customlistadapter. updating arraylist new data using handler runs asynktask every 6 seconds , calling notifydatasetchanged() on oppostexcute(). data shown in listview , updated , ok. if scroll listview or down, application crashes.
these global variables:
listview lv1; swiperefreshlayout mswiperefreshlayout; posttask posttask = new posttask(); stockquotelistadapter stockquotelistadapter; arraylist<stockquote> stockslist=new arraylist<stockquote>(); and activity oncreate:
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { result = inflater.inflate(r.layout.indeces_fragment,container,false); mswiperefreshlayout = (swiperefreshlayout) result.findviewbyid(r.id.activity_main_swipe_refresh_layout); lv1 = (listview) result.findviewbyid(r.id.stocks_list); stockquotelistadapter = new stockquotelistadapter(result.getcontext(), stockslist); lv1.setadapter(stockquotelistadapter); mswiperefreshlayout.setcolorschemeresources(r.color.orange, r.color.green, r.color.blue); mswiperefreshlayout.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() { @override public void onrefresh() { //mswiperefreshlayout.setrefreshing(false); } }); usehandler(); return result; } and handler runs asynctask every 6 seconds:
handler mhandler; public void usehandler() { mhandler = new handler(); mhandler.postdelayed(mrunnable, 2000); } private runnable mrunnable = new runnable() { @override public void run() { try { new posttask().execute(); mhandler.postdelayed(mrunnable, 6000); } catch (exception ex) { ex.printstacktrace(); } } }; this asynctask:
private class posttask extends asynctask<void, void, arraylist<stockquote>> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected arraylist<stockquote> doinbackground(void... params) { stockslist.clear(); ....some json parsing here return stockslist; } }catch (exception ex) { ex.printstacktrace(); } return null; } @override protected void onpostexecute(arraylist<stockquote> stockslist) { super.onpostexecute(stockslist); stockquotelistadapter.notifydatasetchanged(); } am doing wrong since im new android.
error:
on: invalid index 8, size 0 @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255) @ java.util.arraylist.get(arraylist.java:308) @ profestock.moe.org.swipewithtabs.stocks.stockquotelistadapter.getview(stockquotelistadapter.java:58) @ android.widget.abslistview.obtainview(abslistview.java:2347) @ android.widget.listview.makeandaddview(listview.java:1864) @ android.widget.listview.filldown(listview.java:698) @ android.widget.listview.fillgap(listview.java:662) @ android.widget.abslistview.trackmotionscroll(abslistview.java:4991) @ android.widget.abslistview.scrollifneeded(abslistview.java:3418) @ android.widget.abslistview.ontouchmove(abslistview.java:3801) @ android.widget.abslistview.ontouchevent(abslistview.java:3632) @ android.view.view.dispatchtouchevent(view.java:8471) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2399) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2092)
you're getting concurrentmodificationexception because have 2 threads accessing stockslist. doinbackground running on separate thread, , adding data list @ exact same time adapter reading it.
the solution not share arraylist. in doinbackground, should create new arraylist contains new data json. then, in onpostexecute (which gets called on main thread) call stockslist.clear() , add parameter stockslist (which should rename result avoid confusion) this.stockslist before calling notifydatasetchanged().
Comments
Post a Comment