【问题标题】:Error: Trying to requery an already closed cursor错误:试图重新查询一个已经关闭的游标
【发布时间】:2012-06-23 16:23:04
【问题描述】:

我遇到了一个错误,但我似乎找不到解决方案。我的应用程序在 ICS 4.0.3 上运行,但它在 Android 3.2 上返回错误。这是我的MainActivity.java 文件中的代码:

SQLiteDatabase db = databaseHelper.getReadableDatabase();
         Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, new String[]{"_id","isim","icerik"}, null, null, null, null, null);
         startManagingCursor(cursor);
         tts=0;



         while(cursor.moveToNext()){
            if(tts==0)
             {            
            array_spinner=new String[cursor.getCount()+1];
            array_spinner[tts]= "FAVORİ";
             }
            array_spinner2[tts+1]= cursor.getString((cursor.getColumnIndex("isim")));
            array_spinner3[tts+1]= cursor.getString((cursor.getColumnIndex("icerik")));
             tts++;
            } 

         ss2 = (Spinner) findViewById(R.id.spinner2);
         ArrayAdapter<Object> adapter  = new ArrayAdapter<Object>(this,R.layout.row2, R.id.weekofday2, array_spinner2);
            ss2.setAdapter(adapter);
        } catch (Exception e) {}

关于如何解决这个问题有什么建议吗?

【问题讨论】:

    标签: android android-sqlite android-cursor


    【解决方案1】:

    某处,您正在关闭您传递给startManagingCursor()Cursor。在关闭Cursor 之前调用stopManagingCursor()

    请注意,startManagingCursor() 已弃用。当您的数据发生变化时,不要使用它,而是在后台运行另一个查询(例如,AsyncTaskCursorLoader)。

    另外,您可以考虑使用CursorAdapter,例如SimpleCursorAdapter,而不是手动将所有数据转换为对象并使用ArrayAdapter

    另外,如果你要使用ArrayAdapter,请用更具体的类声明它(例如,ArrayAdapter&lt;String&gt;,而不是ArrayAdapter&lt;Object&gt;

    【讨论】:

    • 使用 CursorAdapter 时出现此错误。对我来说,情况不同。我已运行应用程序并按下 Home 按钮,然后使用最近的应用程序再次运行我的应用程序,此异常将引发。我认为更好的方法是使用 CursorLoader
    • 在关闭Cursor 之前调用stopManagingCursor 为我修复了错误!谢谢@CommonsWare
    【解决方案2】:

    输入它可能对你有用:

        @Override
        protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
    
            cursor.requery();
        }
    

    【讨论】:

      【解决方案3】:

      这个错误似乎是由于不正确使用不推荐使用的方法引起的

      startManagingCursor()
      

      不能 100% 确定正确调用 stopManagingCursor() 会解决问题,因为它是非常偶然的。

      我建议避免使用它并坚持使用 CursorLoader 和 LoaderManager。

      【讨论】:

        猜你喜欢
        • 2013-04-18
        • 1970-01-01
        • 2011-08-20
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2011-08-28
        • 2015-03-06
        • 1970-01-01
        相关资源
        最近更新 更多