Android java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteQuery:SELECT*FROM

Android java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteQuery:SELECT*FROM,android,sqlite,exception,cursor,Android,Sqlite,Exception,Cursor,我的申请有一个错误 我是android应用程序的新手,所以我现在不知道怎么了 这在日志中: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM pets 我有一个SQLite数据库,其中包含以下方法: public List<Pet> getAllPets() { List<Pet> petList = n

我的申请有一个错误

我是android应用程序的新手,所以我现在不知道怎么了

这在日志中:

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM pets
我有一个SQLite数据库,其中包含以下方法:

public List<Pet> getAllPets() {
    List<Pet> petList = new ArrayList<Pet>();
    String selectQuery = "SELECT * FROM " + TABLE_PETS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            Pet pet = new Pet();
            pet.setID(Integer.parseInt(cursor.getString(0)));
            pet.setName(cursor.getString(1));
            pet.setAge(cursor.getString(2));
            petList.add(pet);
        } while (cursor.moveToNext());
    }
    return petList;     
}

首先,您应该在使用完光标后关闭光标(这可能会解决您的问题)

public List getAllPets(){
List petList=new ArrayList();
String selectQuery=“SELECT*FROM”+表\u;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
做{
宠物=新宠物();
pet.setID(Integer.parseInt(cursor.getString(0));
pet.setName(cursor.getString(1));
pet.setAge(cursor.getString(2));
petList.add(pet);
}while(cursor.moveToNext());
}
//使用完光标后将其关闭
cursor.close();
返回petList;
}

首先,您应该在使用完光标后关闭光标(这可能会解决您的问题)

public List getAllPets(){
List petList=new ArrayList();
String selectQuery=“SELECT*FROM”+表\u;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
做{
宠物=新宠物();
pet.setID(Integer.parseInt(cursor.getString(0));
pet.setName(cursor.getString(1));
pet.setAge(cursor.getString(2));
petList.add(pet);
}while(cursor.moveToNext());
}
//使用完光标后将其关闭
cursor.close();
返回petList;
}

最后,我通过替换getPetsCount方法解决了这个问题:

public int getPetsCount() {
    String countQuery = "SELECT * FROM " + TABLE_PETS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    return cursor.getCount();
}
public int getPetsCount() {
    String countQuery = "SELECT * FROM " + TABLE_PETS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int count = cursor.getCount();
    cursor.close();
    db.close();
    return count;

最后,我通过替换getPetsCount方法解决了这个问题:

public int getPetsCount() {
    String countQuery = "SELECT * FROM " + TABLE_PETS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    return cursor.getCount();
}
public int getPetsCount() {
    String countQuery = "SELECT * FROM " + TABLE_PETS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int count = cursor.getCount();
    cursor.close();
    db.close();
    return count;

可以发布getPetScont()方法吗?关闭光标并关闭数据库ex:db.close()。您可能也在其他地方调用了db创建方法。这就是你得到的例外,我觉得。@kumar我应该在哪里关闭数据库和光标?当fiddler回答我的问题时,我试图关闭光标,但没有任何帮助。它仍然抛出相同的异常…如果db.getpetscont();此方法具有db closing语句。然后,在执行db.getAllPets()之前,必须再次获得数据库连接。所以最好在getPetsCount()中关闭db。在调用getAllPets()方法之前再次打开数据库连接。是否可以发布getPetsCount()方法?关闭光标并关闭数据库ex:db.close()。您可能也在其他地方调用了db创建方法。这就是你得到的例外,我觉得。@kumar我应该在哪里关闭数据库和光标?当fiddler回答我的问题时,我试图关闭光标,但没有任何帮助。它仍然抛出相同的异常…如果db.getpetscont();此方法具有db closing语句。然后,在执行db.getAllPets()之前,必须再次获得数据库连接。所以最好在getPetsCount()中关闭db。在调用getAllPets()方法之前再次打开数据库连接。