0

i have an error in my application.

I'm new in making apps for android, so i don´t now what's wrong...

this is in the log:

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM pets

i have a SQLite database in which is this method:

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

and then i'am using this method from database in other activity to fill the ListView by the names of every pet....

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    PetListView = (ListView)findViewById(R.id.list);

    MyDatabaseHelper db = new MyDatabaseHelper(this);
    String [] items = new String[db.getPetsCount()];

    List<Pet> pets = db.getAllPets();
    for (int i = 0; i < db.getPetsCount(); i++) {
        items[i] = pets.get(i).getName();                   
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, items);
    PetListView.setAdapter(adapter);
}

when this activity starts the application crashes down..

Please, can you tell me what's worng? How can i fix it? Thanks a much for help.

EDIT:

getPetsCount() method:

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

    return cursor.getCount();
}
4

2 に答える 2

3

i solved this by myself at the end by replace the getPetsCount method:

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;
于 2012-12-18T17:24:21.060 に答える
0

First, you should close your cursor once you have finished to use it (that may solve your issue)

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

// Close cursor when finished using it
cursor.close();

return petList;     
}
于 2012-12-07T15:09:59.583 に答える