Search code examples
androidandroid-sqliteandroid-cursor

Error: Trying to requery an already closed cursor


I'm running into an error but I can't seem to find a solution. My application is working on ICS 4.0.3 but it returns the error on Android 3.2. Here is the code in my MainActivity.java file:

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) {}

Any suggestions on how to solve this problem?


Solution

  • Somewhere, you are closing the Cursor you are passing to startManagingCursor(). Call stopManagingCursor() before closing the Cursor.

    Note that startManagingCursor() is deprecated. Instead of using that, when your data changes, run another query in the background (e.g., AsyncTask, CursorLoader).

    Also, you might consider using a CursorAdapter, like SimpleCursorAdapter, rather than manually converting all of the data into objects and using an ArrayAdapter.

    Also, if you are going to use ArrayAdapter, please declare it with a more concrete class (e.g., ArrayAdapter<String>, not ArrayAdapter<Object>.