Android - java.lang.IllegalStateException:尝试重新打开已经关闭的对象:SQLiteQuery:select * from table where rowid = 40495

时间:2017-11-08 15:51:13

标签: java android sqlite

在我的活动中,我请求API并从中获取数据并在我的SQLite数据库中插入数据。然后我称之为这个方法:

public long getResultCount() {
    SQLiteDatabase db = this.getReadableDatabase();
    long cnt  = DatabaseUtils.queryNumEntries(db, TABLE_NAME);
    return cnt;
}

此方法在其他java类中,它在我的活动中正常工作。调用此方法后,我调用方法:

public ArrayList<Integer> getId(int i, int j) {

    ArrayList<Integer> array_list = new ArrayList<Integer>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from "
            + TABLE_NAME
            + " limit " + i + "," + j
            , null);
    res.moveToFirst();

    while (!res.isAfterLast()) {
        array_list.add(Integer.parseInt(res.getString(res.getColumnIndex(COLUMN_ID))));
        res.moveToNext();
    }
    return array_list;
}

当我在此之后编写此代码时:

temp = db.getId(min * 5, min * 5 + 5);

for (int i = 0; i < temp.size(); i++) {

        Cursor rs = db.getResult(temp.get(i));
        rs.moveToFirst();
        ...
        rs.close();
    }

发生错误:

  

11-08 19:16:35.108 15365-15365 /? E / AndroidRuntime:致命异常:主要                                                      过程:com.myapp.app,PID:15365                                                      java.lang.IllegalStateException:尝试重新打开一个已经关闭的对象:SQLiteQuery:select * from table其中rowId = 40495                                                          在android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)                                                          在android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)                                                          在android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:146)                                                          在android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:135)                                                          在android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)                                                          在android.database.AbstractCursor.moveToFirst(AbstractCursor.java:243)                                                          在com.myapp.app.MyFragment.populateListView(MyFragment.java:327)                                                          在com.myapp.app.MyFragment $ GetArticles.onPostExecute(MyFragment.java:693)                                                          在com.myapp.app.MyFragment $ GetArticles.onPostExecute(MyFragment.java:497)                                                          在android.os.AsyncTask.finish(AsyncTask.java:636)                                                          在android.os.AsyncTask.access $ 500(AsyncTask.java:177)                                                          在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:653)                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                          在android.os.Looper.loop(Looper.java:135)                                                          在android.app.ActivityThread.main(ActivityThread.java:5290)                                                          at java.lang.reflect.Method.invoke(Native Method)                                                          在java.lang.reflect.Method.invoke(Method.java:372)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:911)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)

getResult 方法是这样的:

public Cursor getResult(int Id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME + " where " + COLUMN_ID + "=" + Id + "", null);
    res.close();
    return res;
}

3 个答案:

答案 0 :(得分:0)

您在res.close

之前忘记了return array_list;

答案 1 :(得分:0)

您的getResult()方法返回已关闭Cursor。你不能真正使用封闭游标。

close()中的getResult()光标不是import MyChatWidget from 'components/MyChatWidget'; export default { widgets: { MyChatWidget: { render: (args) => { ReactDOM.render( <MyChatWidget /> ); }, logout: () => { // this function should call a the logout in the MyChatWidget // React Component } } } 。或者在关闭之前捕获所需的数据并返回数据,而不是光标。

答案 2 :(得分:0)

for (int i = 0; i < temp.size(); i++) {

        Cursor rs = db.getResult(temp.get(i));
        rs.moveToFirst();
        ...
        rs.close();
    }

关闭光标有什么条件吗?如果不是 - 在第一次迭代后光标将被关闭,你将在第二次迭代时获得异常。