Android:尝试重新打开已经关闭的对象:SQLiteDatabase:

时间:2017-03-17 00:11:40

标签: android

我正在使用本教程https://www.developerfeed.com/simple-quiz-game-andriod/

创建一个测验应用

错误即将到来......

我在DB Class中遇到错误:

package tk.davidev.android.ews;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "quiz";
    // tasks table name
    private static final String TABLE_QUEST = "quest";
    // tasks Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer"; //correct option
    private static final String KEY_OPTA= "opta"; //option a
    private static final String KEY_OPTB= "optb"; //option b
    private static final String KEY_OPTC= "optc"; //option c
    private SQLiteDatabase dbase;
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        dbase=db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
                + " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
                +KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
        db.execSQL(sql);
        Question q1=new Question("Qual é o primeiro posto?", "Candidato", "Arauto", "Escudeiro", "A");
        this.addQuestion(q1);
        Question q2=new Question("Quantos são os requisitos mínimos?", "10", "6", "5", "C");
        this.addQuestion(q2);
        Question q3=new Question("Em que foi inspirado o hino firmando propósitos?", "Nas cores da Organização", "Na organização.", "No serviço real.", "Nos embaixadores");
        this.addQuestion(q3);
        Question q4=new Question(
                "No Plano de Salvação apresentado no manual do candidato, qual é o versículo do  título “O plano de Deus para a salvação de  todos”?",
                "João 3:16",
                "II Corítios 5.20",
                "Este plano não está presente.",
                "A");
        this.addQuestion(q4);
        Question q5=new Question(
                "Qual o nome dado ao conjunto: “Tema, Divisa, Compromisso, Significado do Nome de Embaixador do Rei e Hino Firmando Propósitos”?",
                "Embaixada",
                "Requisitos Mínimos",
                "Planos de Salvação.",
                "B");
        this.addQuestion(q5);
        db.close();
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
        onCreate(db);
    }
    public void addQuestion(Question quest) {
        ContentValues values = new ContentValues();
        values.put(KEY_QUES, quest.getQUESTION());
        values.put(KEY_ANSWER, quest.getANSWER());
        values.put(KEY_OPTA, quest.getOPTA());
        values.put(KEY_OPTB, quest.getOPTB());
        values.put(KEY_OPTC, quest.getOPTC());
        dbase.insert(TABLE_QUEST, null, values);
    }
    public List<Question> getAllQuestions() {
        List<Question> quesList = new ArrayList<Question>();
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        dbase=this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                Question quest = new Question();
                quest.setID(cursor.getInt(0));
                quest.setQUESTION(cursor.getString(1));
                quest.setANSWER(cursor.getString(2));
                quest.setOPTA(cursor.getString(3));
                quest.setOPTB(cursor.getString(4));
                quest.setOPTC(cursor.getString(5));
                quesList.add(quest);
            } while (cursor.moveToNext());
        }
// return quest list
        return quesList;
    }
    public int rowcount()
    {
        int row=0;
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        row=cursor.getCount();
        return row;
    }
}

错误是: IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/tk.davidev.android.ews/databases/quiz

我是Android的新程序员,帮帮我!

什么是hapenning?

抱歉BAD英语,我是巴西人。

0 个答案:

没有答案