在创建方法不被调用

时间:2018-06-18 14:47:19

标签: java android oncreate

即使在卸载应用程序并重新安装后,onCreate方法也未被调用

如何解决问题?

这里是代码:

public class QuizDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MyAwesomeQuiz1.db";
private static final int DATABASE_VERSION = 1;

private SQLiteDatabase db;

public QuizDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    //getReadableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    this.db = db;

    final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
            QuestionsTable.TABLE_NAME + " ( " +
            QuestionsTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            QuestionsTable.COLUMN_QUESTION + " TEXT, " +
            QuestionsTable.COLUMN_OPTION1 + " TEXT, " +
            QuestionsTable.COLUMN_OPTION2 + " TEXT, " +
            QuestionsTable.COLUMN_OPTION3 + " TEXT, " +
            QuestionsTable.COLUMN_OPTION4 + " TEXT, " +
            QuestionsTable.COLUMN_ANSWER_NR + " INTEGER" +
            QuestionsTable.COLUMN_TYPE + "TEXT" +
            ")";

    db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
    fillQuestionsTable();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + QuestionsTable.TABLE_NAME);
    onCreate(db);
}

private void fillQuestionsTable() {
    Question q1 = new Question("A is correct", "A", "B", "C", "D",1,"Ratio");
    addQuestion(q1);
    Question q2 = new Question("B is correct", "A", "B", "C","D", 2,"Ratio");
    addQuestion(q2);
    Question q3 = new Question("C is correct", "A", "B", "C","D",3,"Ratio");
    addQuestion(q3);
    Question q4 = new Question("A is correct again", "A", "B", "C","D",1,"Time");
    addQuestion(q4);
    Question q5 = new Question("B is correct again", "A", "B", "C","D",2,"Time");
    addQuestion(q5);
}

private void addQuestion(Question question) {
    ContentValues cv = new ContentValues();
    cv.put(QuestionsTable.COLUMN_QUESTION, question.getQuestion());
    cv.put(QuestionsTable.COLUMN_OPTION1, question.getOption1());
    cv.put(QuestionsTable.COLUMN_OPTION2, question.getOption2());
    cv.put(QuestionsTable.COLUMN_OPTION3, question.getOption3());
    cv.put(QuestionsTable.COLUMN_OPTION4, question.getOption4());
    cv.put(QuestionsTable.COLUMN_ANSWER_NR, question.getAnswerNr());
    cv.put(QuestionsTable.COLUMN_TYPE, question.getQuid());
    db.insert(QuestionsTable.TABLE_NAME, null, cv);
}

public ArrayList<Question> getAllQuestions() {
    ArrayList<Question> questionList = new ArrayList<>();
    db = getReadableDatabase();
    //String[] column = {"QuestionsTable.COLUMN_QUESTION","QuestionsTable.COLUMN_OPTION1","QuestionsTable.COLUMN_OPTION2","QuestionsTable.COLUMN_OPTION3","QuestionsTable.COLUMN_OPTION4","QuestionsTable.COLUMN_ANSWER_NR"};
    //String selection = "QuestionsTable.COLUMN_TYPE =?";
    String[] params = new String[]{ "Ratio" };
    Cursor c = db.query( QuestionsTable.TABLE_NAME , null,null, null,null,null,null);

    if (c.moveToFirst()) {
        do {
            Question question = new Question();
               question.setQuestion(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_QUESTION)));                    question.setOption1(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION1)));
                question.setOption2(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION2)));
                question.setOption3(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION3)));
                question.setOption4(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION4)));
                question.setAnswerNr(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_ANSWER_NR)));
                question.setQuid(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_TYPE)));
                questionList.add(question);
        } while (c.moveToNext());
    }

    c.close();
    return questionList;
}
}

Quizactivity.java类

 if (savedInstanceState == null) {
        QuizDbHelper dbHelper = new QuizDbHelper(this);
        questionList = dbHelper.getAllQuestions();
        questionCountTotal = questionList.size();
        //Collections.shuffle(questionList);

        showNextQuestion();

    } else {
        questionList = 
savedInstanceState.getParcelableArrayList(KEY_QUESTION_LIST);
        questionCountTotal = questionList.size();
        questionCounter = savedInstanceState.getInt(KEY_QUESTION_COUNT);
        currentQuestion = questionList.get(questionCounter - 1);
        score = savedInstanceState.getInt(KEY_SCORE);
        timeLeftInMillis = savedInstanceState.getLong(KEY_MILLIS_LEFT);
        answered = savedInstanceState.getBoolean(KEY_ANSWERED);

        if (!answered) {
            startCountDown();
        } else {
            updateCountDownText();
            showSolution();
        }`enter code here`

0 个答案:

没有答案