Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery

Hy everybody,

I have an error and i don't know what is wrong

Here is my error from log

java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT display_name, _id FROM view_data_restricted data WHERE (1) AND (data1 =? AND mimetype='vnd.android.cursor.item/group_membership' AND display_name like '%r%') ORDER BY display_name)

and here is my code

public Cursor runQuery(CharSequence constraint) {
filter = nome.getText().toString();

try{
tempCurs = getContentResolver().query(ContactsContract.Groups.CONTENT_URI,
    new String[]{ContactsContract.Groups._ID,ContactsContract.Groups.TITLE},
    ContactsContract.Groups.ACCOUNT_NAME + " =? " + " AND " + ContactsContract.Groups.TITLE + " !=? ",
    new String[]{accountName,nomeGrupo},
    null
    );      
if(tempCurs.moveToFirst())
    do{
        cursorContactosGrupos = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.CommonDataKinds.GroupMembership.DISPLAY_NAME, ContactsContract.CommonDataKinds.GroupMembership._ID},
                ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " =? AND " + Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.CommonDataKinds.GroupMembership.DISPLAY_NAME + " like '%" + filter + "%'" ,
                new String[]{String.valueOf(tempCurs.getLong(0))},
                ContactsContract.CommonDataKinds.GroupMembership.DISPLAY_NAME
                );
         //Log.w(SocioEdit.class.getName(), "->" + cursorContactosGrupos.getString(cursorContactosGrupos.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)));
            }while(tempCurs.moveToNext());
        }finally{
            if(cursorContactosGrupos != null && tempCurs != null && !cursorContactosGrupos.isClosed() && !tempCurs.isClosed()){
                cursorContactosGrupos.close();
                tempCurs.close();
            }   
        }
        return cursorContactosGrupos;
    }       
});

What I'm doing wrong?And how can fix? Thanks for help

like image 972
Ricardo Graça Avatar asked Jun 27 '12 09:06

Ricardo Graça


1 Answers

The error is possibly because you are returning a Cursor that you have already closed in the finally block and you may be trying to use the returned value.

Change the finally block to the following:

finally{
    if(tempCurs != null && !tempCurs.isClosed()){
        tempCurs.close();
    }   
}

and remember to close the returned Cursor from the calling method.

like image 167
user1417430 Avatar answered Nov 14 '22 16:11

user1417430