【问题标题】:android finalizing a cursor that has not been deactivated or closedandroid完成一个尚未停用或关闭的游标
【发布时间】:2012-01-22 13:28:50
【问题描述】:

似乎我无法修复此错误消息。 我正确关闭了光标..我什至尝试创建一个具有不同引用名称的新光标..但这也不起作用..我做错了什么?

12-17 10:09:14.107:E/Cursor(277):完成尚未停用或关闭的光标。数据库 = /data/data/com.lernapp.src/databases/LernApp,表 = 答案,查询 = 选择答案,更正 FROM 答案 WHERE TestId = ? AND TestPageId = ?

private void getRowData(LernAppOpenHelper myDbHelper) {
        String[] columnsTestPage = {"TestPageId", "Question","Picture"};
        Cursor cursor = myDbHelper.getQuery("TestPage", columnsTestPage, "TestId = ?", new String[]{testNummer}, null, null, null); 
        startManagingCursor(cursor);
        cursor.moveToFirst();

        while(!cursor.isAfterLast()){   
            int testPageId = cursor.getInt(cursor.getColumnIndex("TestPageId"));
            String question = cursor.getString(cursor.getColumnIndex("Question"));
            String picture = cursor.getString(cursor.getColumnIndex("Picture"));

            this.testPages.add(new TestPage(testPageId, question, picture));
            cursor.moveToNext();
        }
        cursor.close();
        columnsTestPage = null;

        String[] columnsAnswers = {"Answer", "Correct"};

        for(TestPage p: testPages){
            cursor = myDbHelper.getQuery("Answers", columnsAnswers, "TestId = ? AND TestPageId = ?", new String[]{testNummer, p.getTestPageId()}, null, null, null);
            cursor.moveToFirst();
            while(!cursor.isAfterLast()){
                String answer = cursor.getString(cursor.getColumnIndex("Answer"));
                int correct = cursor.getInt(cursor.getColumnIndex("Correct"));
                p.setAnswer(answer, correct);
                cursor.moveToNext();
            }           
        }   

        cursor.close();
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您使用startManagingCursor(cursor);,则无需关闭光标,Android 会为您完成。 要么删除 startManagingCursor(cursor);,要么删除 cursor.close(); 引用。

    编辑: 试试这个:

    private void getRowData(LernAppOpenHelper myDbHelper) {
            String[] columnsTestPage = {"TestPageId", "Question","Picture"};
            Cursor cursor = myDbHelper.getQuery("TestPage", columnsTestPage, "TestId = ?", new String[]{testNummer}, null, null, null);
    
            cursor.moveToFirst();
            while(!cursor.isAfterLast()){   
                int testPageId = cursor.getInt(cursor.getColumnIndex("TestPageId"));
                String question = cursor.getString(cursor.getColumnIndex("Question"));
                String picture = cursor.getString(cursor.getColumnIndex("Picture"));
    
                this.testPages.add(new TestPage(testPageId, question, picture));
                cursor.moveToNext();
            }
            cursor.close();
            columnsTestPage = null;
    
            String[] columnsAnswers = {"Answer", "Correct"};
    
            for(TestPage p: testPages){
                cursor = myDbHelper.getQuery("Answers", columnsAnswers, "TestId = ? AND TestPageId = ?", new String[]{testNummer, p.getTestPageId()}, null, null, null);
                cursor.moveToFirst();
                while(!cursor.isAfterLast()){
                    String answer = cursor.getString(cursor.getColumnIndex("Answer"));
                    int correct = cursor.getInt(cursor.getColumnIndex("Correct"));
                    p.setAnswer(answer, correct);
                    cursor.moveToNext();
                }
                cursor.close(); // Close the cursor here before the next loop
            }   
        }
    

    【讨论】:

    • 这不会改变任何东西..如果我只使用 startManagingCursor 或 cursor.close(),会出现相同的错误消息..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多