java.lang.IllegalStateException:尝试重新打开已经关闭的对象:android.database.sqlite.SQLiteQuery

时间:2012-06-27 09:26:25

标签: android exception cursor android-contacts

Hy大家,

我有错误,我不知道出了什么问题

这是我在日志中的错误

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)

这是我的代码

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

我做错了什么?如何解决? 谢谢你的帮助

2 个答案:

答案 0 :(得分:11)

错误可能是因为您返回的Cursor已在finally块中关闭,而您可能正在尝试使用返回的值。

finally块更改为以下内容:

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

并记住从调用方法中关闭返回的Cursor

答案 1 :(得分:-1)

尝试删除以下代码行,然后再次检查

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

添加而不是

finally{
                if(cursorContactosGrupos != null && tempCurs != null && !cursorContactosGrupos.isClosed() && !tempCurs.isClosed()){

                    tempCurs.close();
                }