java - Why is my ListView method throwing a 'Stale object state' exception? -
java - Why is my ListView method throwing a 'Stale object state' exception? -
currently within app getting stale object exception when phone call method updates listview database.
i have been careful close of cursors when querying database, manage cursor when returned through utilize of 'startmanagingcursor' still issue.
at moment, when add together new item database, recall listview update method within private method of class. object throwing exception.
i have read of people 'refreshing' session, , believe database versions come this? overall i'm stuck , cant solve this.
updated error:
02-13 13:23:06.588: e/androidruntime(287): fatal exception: main 02-13 13:23:06.588: e/androidruntime(287): android.database.staledataexception: access closed cursor 02-13 13:23:06.588: e/androidruntime(287): @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:217) 02-13 13:23:06.588: e/androidruntime(287): @ android.database.abstractwindowedcursor.getint(abstractwindowedcursor.java:84) 02-13 13:23:06.588: e/androidruntime(287): @ com.example.flybase2.shoppinglist.updatetotal(shoppinglist.java:248) 02-13 13:23:06.588: e/androidruntime(287): @ com.example.flybase2.shoppinglist.access$1(shoppinglist.java:231) 02-13 13:23:06.588: e/androidruntime(287): @ com.example.flybase2.shoppinglist$2.onclick(shoppinglist.java:350) 02-13 13:23:06.588: e/androidruntime(287): @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:158) 02-13 13:23:06.588: e/androidruntime(287): @ android.os.handler.dispatchmessage(handler.java:99) 02-13 13:23:06.588: e/androidruntime(287): @ android.os.looper.loop(looper.java:123) 02-13 13:23:06.588: e/androidruntime(287): @ android.app.activitythread.main(activitythread.java:4627) 02-13 13:23:06.588: e/androidruntime(287): @ java.lang.reflect.method.invokenative(native method) 02-13 13:23:06.588: e/androidruntime(287): @ java.lang.reflect.method.invoke(method.java:521) 02-13 13:23:06.588: e/androidruntime(287): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 02-13 13:23:06.588: e/androidruntime(287): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 02-13 13:23:06.588: e/androidruntime(287): @ dalvik.system.nativestart.main(native method)
this class calls listview:
package com.example.flybase2; import android.app.alertdialog; import android.app.alertdialog.builder; import android.app.dialog; import android.app.listactivity; import android.content.dialoginterface; import android.database.cursor; import android.os.bundle; import android.support.v4.widget.simplecursoradapter; import android.text.editable; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.linearlayout; import android.widget.listview; import android.widget.textview; public class shoppinglist extends listactivity implements onclicklistener { button additem; listview showitems; simplecursoradapter cursoradapter; long itemid; edittext totalprice; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.shoppinglistlayout); additem = (button) findviewbyid(r.id.btnadditem); showitems = (listview)findviewbyid(android.r.id.list); totalprice = (edittext)findviewbyid(r.id.totallistprice); additem.setonclicklistener(this); setlist(); } error**********************************8 @override public void onclick(view clickedadd) { show(); } @override protected void onlistitemclick(listview l, view v, int position, long idd) { super.onlistitemclick(l, v, position, idd); itemid = idd; final charsequence[] items = {"edit item", "delete item", "show purchased item"}; builder alertdialogbuilder = new alertdialog.builder(shoppinglist.this); alertdialogbuilder.settitle("item options:"); alertdialogbuilder.setitems(items, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int item) { if (items[item].equals("edit item")) { alertdialog.builder builder = new alertdialog.builder(shoppinglist.this); builder.settitle("edit item"); dbhandlershop setedit = new dbhandlershop(shoppinglist.this, null, null); setedit.open(); string itemname = setedit.getitem(itemid); int itemamount = setedit.getitemquan(itemid); int itemprice = setedit.getitemcost(itemid); setedit.close(); linearlayout layout = new linearlayout(shoppinglist.this); layout.setorientation(linearlayout.vertical); final edittext titlebox = new edittext(shoppinglist.this); titlebox.settext(itemname); titlebox.sethint("item name:"); layout.addview(titlebox); final edittext quantitybox = new edittext(shoppinglist.this); quantitybox.settext(integer.tostring(itemamount)); quantitybox.sethint("item quantity"); layout.addview(quantitybox); final edittext pricebox = new edittext(shoppinglist.this); pricebox.settext(integer.tostring(itemprice)); pricebox.sethint("item price."); layout.addview(pricebox); builder.setview(layout); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { editable valueitem = titlebox.gettext(); editable valueamount = quantitybox.gettext(); editable valueprice = pricebox.gettext(); string itemdescription = valueitem.tostring(); string s = valueamount.tostring(); int itemamount = integer.parseint(s); string = valueprice.tostring(); int itemprice = integer.parseint(a); try{ dbhandlershop update = new dbhandlershop(shoppinglist.this, null, null); update.open(); update.updateitem(itemid, itemdescription, itemamount, itemprice); update.close(); } catch(exception e) { dialog e1 = new dialog(shoppinglist.this); e1.settitle("item unsuccesfully updated"); textview txt = new textview(shoppinglist.this); txt.settext("success"); e1.setcontentview(txt); e1.show(); } { dialog e1 = new dialog(shoppinglist.this); e1.settitle("item succesfully updated"); textview txt = new textview(shoppinglist.this); txt.settext("success"); e1.setcontentview(txt); e1.show(); setlist(); int cost = updatetotal(); totalprice.settext(cost); } } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { } }); builder.show(); } else if (items[item].equals("delete item")) { dbhandlershop delete = new dbhandlershop(shoppinglist.this,null, null); delete.open(); delete.deleteitem(itemid); delete.close(); setlist(); } else if (items[item].equals("show purchased item")) { } } }); alertdialogbuilder.show(); } private void setlist() { dbhandlershop dbshop = new dbhandlershop(this, null, null); dbhandlershop searchitems = new dbhandlershop(this, null, null); searchitems.open(); cursor cursor = searchitems.getitems(); startmanagingcursor(cursor); string [] = new string [] {dbshop.key_itemshop, dbshop.key_itemnum, dbshop.key_itemprice}; int [] = new int [] {r.id.txtsetitem, r.id.txtsetamount, r.id.txtsetprice}; cursoradapter = new simplecursoradapter(this, r.layout.setshoppinglist, cursor, from, to); showitems.setadapter(cursoradapter); } private int updatetotal() { dbhandlershop total = new dbhandlershop(this, null, null); int totalprice = 0; total.open(); cursor totalprices = total.gettotals(); total.close(); if (totalprices != null) { startmanagingcursor(totalprices); if (totalprices.movetofirst()) { { int cost = totalprices.getint(3); totalprice += cost; } while (totalprices.movetonext()); homecoming totalprice; } } homecoming 0; } private void show() { alertdialog.builder builder = new alertdialog.builder(shoppinglist.this); builder.settitle("enter item details:"); linearlayout layout = new linearlayout(this); layout.setorientation(linearlayout.vertical); final edittext titlebox = new edittext(this); titlebox.sethint("item name:"); layout.addview(titlebox); final edittext quantitybox = new edittext(this); quantitybox.sethint("item quantity"); layout.addview(quantitybox); final edittext pricebox = new edittext(this); pricebox.sethint("item price."); layout.addview(pricebox); builder.setview(layout); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { seek { editable valueitem = titlebox.gettext(); editable valueamount = quantitybox.gettext(); editable valueprice = pricebox.gettext(); string itemdescription = valueitem.tostring(); string s = valueamount.tostring(); int itemamount = integer.parseint(s); string = valueprice.tostring(); int itemprice = integer.parseint(a); dbhandlershop additem = new dbhandlershop(shoppinglist.this, null, null); additem.open(); additem.insertitems(itemdescription, itemamount, itemprice); additem.close(); } catch(exception e) { dialog e1 = new dialog(shoppinglist.this); e1.settitle("item unsuccesfully added"); textview txt = new textview(shoppinglist.this); txt.settext("success"); e1.setcontentview(txt); e1.show(); } { dialog e = new dialog(shoppinglist.this); e.settitle("item succesfully added."); textview txt = new textview(shoppinglist.this); txt.settext("success"); e.setcontentview(txt); e.show(); setlist(); int cost = updatetotal(); totalprice.settext(cost); } } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { } }); builder.show(); } }
this cursor returned database:
public cursor getitems() { string [] columns = new string[]{key_rowshopid, key_itemshop, key_itemnum, key_itemprice}; cursor c = ourdatabase.query(database_tableshop, columns, null, null, null, null, null); if(c != null && c.getcount() > 0) { c.movetofirst(); c.close(); homecoming c; } c.close(); homecoming null; }
02-13 11:37:52.013: e/androidruntime(287): android.database.staledataexception: access closed cursor
your problem trying access info on closed cursor
. not allowed remove line c.close()
, should works.
note: recommended close cursors, datasources in onpause()
or ondestroy()
methods.
update: here edited method, re-create , paste , seek again. should works.
public cursor getitems() { string [] columns = new string[] {key_rowshopid, key_itemshop, key_itemnum, key_itemprice}; cursor c = ourdatabase.query(database_tableshop, columns, null, null, null, null, null); if (c.movetofirst()) { homecoming c; } homecoming null; } public void ondestroy() { super.ondestroy(); closesources(); } private void closesources() { if (c != null) { c.close(); } if (db != null) { db.close(); } }
java android staleobjectstate staledataexception
Comments
Post a Comment