java.lang.IllegalStateException:尝试重新打开已关闭的对象:android.database.sqlite.SQLiteQuery
大家好 我有一个错误,我不知道是什么错了 这是我的日志错误java.lang.IllegalStateException:尝试重新打开已关闭的对象:android.database.sqlite.SQLiteQuery,android,exception,cursor,android-contacts,Android,Exception,Cursor,Android Contacts,大家好 我有一个错误,我不知道是什么错了 这是我的日志错误 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.
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;
}
});
我做错了什么?怎么能补救?
感谢您的帮助此错误可能是因为您正在返回一个已在
finally
块中关闭的光标
,并且您可能正在尝试使用返回的值
将finally
块更改为以下内容:
finally{
if(tempCurs != null && !tempCurs.isClosed()){
tempCurs.close();
}
}
并记住关闭调用方法返回的
光标
。尝试删除代码的以下行,然后再次检查
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();
}
getContentResolver()返回DB对象是否正确?如果它工作正常,请接受答案:)是的,这是正确的,但告诉您应该找出他在代码中的错误位置。返回闭合游标而不执行任何其他操作的方法是无用的方法,它本身就是错误的。是的,您终于找到了它,干杯:)谢谢你的帮助。您可以向我展示修复代码的最佳方法?+1 for finally{if(tempCurs!=null&&!tempCurs.isClosed()){tempCurs.close();}}如果删除这些行,您将泄漏
游标。这是不正确的。为什么包含此行的方法返回游标如果你想关闭不再使用的游标,你可以使用返回类型为void的方法,我不认为我的答案应该被否决,问题本身是这样的:怎么办?:)我只是在这个函数中使用这两个游标。我不会在另一边打开。我想我不能删除这行,因为光标需要关闭…@Ricardo那么你为什么要在finally block中关闭两个光标并返回关闭的光标?是的。你说得对。我的错误。如果我返回一个关闭的游标,我没有游标的结果