我知道这个问题在SO上已经被问了很多次,但是我无法找出自己的确切问题。
我正在使用以下代码从数据库(Table1)获取数据并根据检索值更新另一个Table2。 在某些android版本中它可以正常工作,但当我尝试使用Android 4.0.3进行测试时,我会得到以下错误: java.lang.IllegalStateException:?.attempt to re-open an already-closed object
at sum_cursor.moveToNext();
.
我在AsyncTask中使用这段代码。
/** Sum of total matched values*/
Cursor sum_cursor = db.gettotalMatchvalue(this);
if(sum_cursor!=null)
{
sum_cursor.moveToFirst();
for(int j=0; j<sum_cursor.getCount();j++)
{
float totalmatchedscore = sum_cursor.getInt(0);
float totalingredients = Float.parseFloat(sum_cursor.getString(sum_cursor.getColumnIndex(APPDatabase.CK_TOTALINCREDIENTS)));
/**average = totalscore/totalingredients*/
double average = totalmatchedscore/totalingredients;
int id = Integer.parseInt(sum_cursor.getString(sum_cursor.getColumnIndex(APPDatabase.CK_ID)));
db.updateAverage(id, average);
sum_cursor.moveToNext(); //Here is the problem
}
}
db.close();
我的更新方法的代码
/** Update average */
public void updateAverage(int id,double average)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CK_FINALVALUE,average);
db.update(TABLE, values,CK_ID+" = "+id , null);
}
我在这里做错了什么?
我知道很多人都会遇到这种情况。你们能帮帮我吗?
谢谢你们的帮助。