samedi 27 juin 2015

attempt to re-open an already-closed object: SQLiteDatabase 1212

in my android app,i m using sqlite database but when using a query to read data from SQLite its giving me error. i want to put the result in a listview.

this my activity :

public class activ5 extends Activity {
ListView etudiants=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activ5);
    BDD base2 = new BDD(activ5.this);
    base2.open();
    ArrayAdapter<Etudiant> etudiantAdapter = new ArrayAdapter<Etudiant>(this, android.R.layout.simple_list_item_activated_1, base2.getAllEtudiant());
    etudiants.setAdapter(etudiantAdapter);
    base2.close();

and this this the code for my database

    private static final String CREATE_BDD_ETUDIANT = "CREATE TABLE " + TABLE_ETUDIANT + " ("
        + COL_ID_ETUDIANT + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NOM + " TEXT NOT NULL, "
        + COL_FILIERE + " TEXT NOT NULL, "+ COL_GROUPE +" TEXT NOT NULL);";
private static final String CREATE_BDD_PROF = "CREATE TABLE " + TABLE_PROF + " ("
        + COL_ID_PROF + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_MODULE + " TEXT NOT NULL, "
        + COL_MATIERE +" TEXT NOT NULL);";
private static final String CREATE_BDD_ABSENCE= "CREATE TABLE " + TABLE_ABSENCE + " ("+ COL_MATIERE_ABSENCE +  " TEXT NOT NULL, "  + COL_ABSENCE + " INTEGER, "
        + COL_NOM_ABSENCE + " TEXT  REFERENCES "+TABLE_ETUDIANT+"("+COL_NOM+")); ";
private static final String CREATE_BDD_COMPTE = "CREATE TABLE " + TABLE_COMPTE + " ("+COL_ID_COMPTE+" INTEGER PRIMARY KEY AUTOINCREMENT, "
        +COL_LOGIN+" TEXT NOT NULL,"+COL_PASSWORD+" TEXT NOT NULL,"
        + COL_ID_PROF_COMPTE + " INTEGER REFERENCES "+TABLE_PROF+"("+COL_ID_PROF+"),"
        + COL_ID_ETUDIANT_COMPTE+ " INTEGER REFERENCES "+TABLE_ETUDIANT+"("+COL_ID_ETUDIANT+"));";
private MaBaseDonne(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static synchronized MaBaseDonne getInstance(Context context) {

    if (sInstance == null) {
        sInstance = new MaBaseDonne(context.getApplicationContext());
    }
    return sInstance;
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_BDD_ETUDIANT);
    db.execSQL(CREATE_BDD_PROF);
    db.execSQL(CREATE_BDD_ABSENCE);
    db.execSQL(CREATE_BDD_COMPTE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE " + TABLE_PROF + ";");
    db.execSQL("DROP TABLE " + TABLE_ETUDIANT + ";");
    db.execSQL("DROP TABLE " + TABLE_ABSENCE+ ";");
    db.execSQL("DROP TABLE " + TABLE_COMPTE+ ";");
    onCreate(db);
}

and this the method where i get the error :

 public List<Etudiant> getAllEtudiant() {
    List<Etudiant> etudiants = new ArrayList<Etudiant>();

    Cursor cursor = bdd.query(maBaseDonne.TABLE_ETUDIANT,null, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Etudiant etudiant = cursorToEtudiant(cursor);
        etudiants.add(etudiant);
        cursor.moveToNext();
    }
    cursor.close();
    return etudiants;
}
public void close() {
    bdd.close();
}


 public Etudiant cursorToEtudiant(Cursor c){

    if (c.getCount() == 0)
        return null;

           c.moveToFirst();

    Etudiant etudiant = new Etudiant();

    etudiant.setId(c.getInt(NUM_COL_ID_ETUDIANT));
    etudiant.setNom(c.getString(NUM_COL_NOM));
    etudiant.setFilierere(c.getString(NUM_COL_FILIERE));
    etudiant.setGroupe(c.getString(NUM_COL_GROUPE));
    c.close();
    return etudiant;

and this is the log file :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radouane.myapplication/com.example.radouane.myapplication.activ5}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM table_etudiant
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5349)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
 Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM table_etudiant
        at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
        at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
        at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:152)
        at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:124)
        at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:214)
        at android.database.AbstractCursor.moveToNext(AbstractCursor.java:245)
        at com.example.radouane.myapplication.BDD.getAllEtudiant(BDD.java:244)
        at com.example.radouane.myapplication.activ5.onCreate(activ5.java:25)
        at android.app.Activity.performCreate(Activity.java:6020)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)

            

i've beeen stuck for all day any help is welcome , thank U``         

Aucun commentaire:

Enregistrer un commentaire