尝试重新打开一个已关闭的对象上:java.lang.IllegalStateException:? [英] Attempt to re-open an already-closed object: java.lang.IllegalStateException:?

查看:727
本文介绍了尝试重新打开一个已关闭的对象上:java.lang.IllegalStateException:?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过很多次这样,但我想不出我确切的问题。

I know this question has asked many times in SO,but i couldn't figure out my exact problem.

我使用下面的code根据检索值从数据库获取(表1)中的数据,并更新另一个表2。它的做工精细一些的Andr​​oid版本,但去检测了Android 4.0.3的时候我。我这个迷途 java.lang.IllegalStateException:?试图重新打开在一个已经关闭的对象 sum_cursor.moveToNext();

I am using the following code to get the data from the database(Table1) and update another Table2 based on retrieval value. Its working fine in some android versions but when i gone to test with Android 4.0.3. I am geting this java.lang.IllegalStateException:?.attempt to re-open an already-closed object at sum_cursor.moveToNext();.

我用这code在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();  

我的更新方法编码

My update method coding

/** 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);   
}

我在做什么错在这里?

What i am doing wrong here?

我知道你们许多人遇到过这种情况。你能帮助我的家伙。

I know many of you come across this situation. Could you help me guys.

感谢您的帮助。

推荐答案

您不能在遍历一个查询的结果更新表。有很好的理由;如果添加会导致更改数据的数据你遍历了什么?您的光标将不会返回有效的数据。

You can't update a table while iterating over the results of a query. There are good reasons for this; what if the data you are adding would cause a change to the data you're iterating over? Your cursor would not return valid data.

试图回表数据存储在 updateAverage()引起的问题。你应该只记得平均值的循环中,然后在最后你完成后循环您的光标一次更新。

Trying to store data back into the table in updateAverage() is causing the problem. You should just remember the average value during your loop, and then update it once at the end after you've finished looping over your cursor.

要进一步解释你得到确切的错误:插入新的数据的行为是导致数据库关闭所有当前打开的游标,作为安全措施。所以,当你调用 sum_cursor.moveToNext()更新平均后,光标是有点惊讶地发现它已经被关闭。

To further explain the exact error you're getting: the act of inserting new data is causing the database to close all the cursors which are currently open, as a safety measure. So when you call sum_cursor.moveToNext() after updating the average, the cursor is a bit surprised to find that it's already been closed.

这篇关于尝试重新打开一个已关闭的对象上:java.lang.IllegalStateException:?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆