【问题标题】:java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQueryjava.lang.IllegalStateException:尝试重新打开一个已经关闭的对象:android.database.sqlite.SQLiteQuery
【发布时间】:2012-06-28 16:53:01
【问题描述】:

大家好,

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

这是我的日志错误

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

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

【问题讨论】:

  • getContentResolver() 返回数据库对象对吗?

标签: android exception android-contacts android-cursor


【解决方案1】:

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

finally 块更改为以下内容:

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

记得关闭调用方法返回的Cursor

【讨论】:

  • 是的,这是真的,而是告诉你应该找出他在代码中做错的地方。
  • 一个方法返回一个关闭的Cursor而不执行任何其他操作是一个无用的方法,它本身就是错误的。
【解决方案2】:

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

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

【讨论】:

  • 如果删除这些行,您将泄漏Cursor。这是不正确的。
  • 十为什么包含此行的方法返回游标如果你想关闭不再使用的游标,你可以有 void 作为返回类型的方法,我不认为我的答案应该对此投反对票,问题本身就是这样的怎么办? :)
  • 我只是在这个函数中使用了这两个游标。我不在另一边打开。我认为我无法删除此行,因为需要关闭游标...
  • @Ricardo 那你为什么在 finally 块中关闭两个光标并返回关闭的光标?
  • 是的。你说得对。我的错。如果我返回一个关闭的游标,我没有游标的结果
猜你喜欢
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多