Android SQLite测验应用

时间:2016-07-10 23:58:09

标签: java android sqlite

我正在尝试创建一个应用程序,让您进行测验,例如,世界历史测验。在主要活动中,用户按下回车按钮选择测验的类别,让我们说我们选择世界历史,然后它应该进入世界历史活动但它崩溃了。到目前为止,这是我的代码,不知道它有什么问题:

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "quiz";
    private static final String TABLE_NAME = "worldhistory";
    private static final String KEY_ID = "id";
    private static final String QUESTIONS = "question";
    private static final String ANSWER = "answer";
    private static final String OPTIONA = "optiona";
    private static final String OPTIONB = "optionb";
    private static final String OPTIONC = "optionc";
    private static final String OPTIOND = "optiond";

    private SQLiteDatabase dba;

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + QUESTIONS + " TEXT, "
                + ANSWER + " TEXT, " + OPTIONA + " TEXT, " + OPTIONB + " TEXT, " + OPTIONC
                + " TEXT, " + OPTIOND + " TEXT)";

        db.execSQL(CREATE_TABLE);

        addQuestions();
    }

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

    // creating the questions, options, and answers
    private void addQuestions() {
        Question q1 = new Question("World War I began in which year?", "1923",
                "1938", "1917", "1914", "D");
        this.addQuestion(q1);

        Question q2 = new Question("Adolf Hitler was born in which country?",
                "France", "Germany", "Austria", "Hungary", "C");
        this.addQuestion(q2);

        //add more questions
    }

    // addding new questions to db
    public void addQuestion(Question question) {
        ContentValues values = new ContentValues();
        values.put(QUESTIONS, question.getQuestion());
        values.put(ANSWER, question.getAnswer());
        values.put(OPTIONA, question.getOptionA());
        values.put(OPTIONB, question.getOptionB());
        values.put(OPTIONC, question.getOptionC());
        values.put(OPTIOND, question.getOptionD());

        dba.insert(TABLE_NAME, null, values);
    }

    public List<Question> getAllQuestions() {
        List<Question> questionList = new ArrayList<Question>();

        String query = "SELECT * FROM " + TABLE_NAME;
        dba = this.getReadableDatabase();
        Cursor cursor = dba.rawQuery(query, 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.setOptionA(cursor.getString(3));
                quest.setOptionB(cursor.getString(4));
                quest.setOptionC(cursor.getString(5));
                quest.setOptionD(cursor.getString(6));

                questionList.add(quest);

            } while (cursor.moveToNext());
        }

        cursor.close();
        dba.close();
        return questionList;
    }

    public int rowCount() {
        int row = 0;
        String query = "SELECT * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        row = cursor.getCount();

        cursor.close();
        return row;
    }
}

Question.java

public class Question {
    private int ID;
    private String Question, optionA, optionB, optionC, optionD, Answer;

    public Question() {
        ID = 0;
        Question = "";
        optionA = "";
        optionB = "";
        optionC = "";
        optionD = "";
        Answer = "";
    }

    public Question(String Question, String optionA, String optionB, String optionC,
                    String optionD, String Answer) {
        this.Question = Question;
        this.optionA = optionA;
        this.optionB = optionB;
        this.optionC = optionC;
        this.optionD = optionD;
        this.Answer = Answer;
    }

    public int getID() {
        return ID;
    }

    public String getQuestion() {
        return Question;
    }

    public String getOptionA() {
        return optionA;
    }

    public String getOptionB() {
        return optionB;
    }

    public String getOptionC() {
        return optionC;
    }

    public String getOptionD() {
        return optionD;
    }

    public String getAnswer() {
        return Answer;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public void setQuestion(String Question) {
        this.Question = Question;
    }

    public void setOptionA(String optionA) {
        this.optionA = optionA;
    }

    public void setOptionB(String optionB) {
        this.optionB = optionB;
    }

    public void setOptionC(String optionC) {
        this.optionC = optionC;
    }

    public void setOptionD(String optionD) {
        this.optionD = optionD;
    }

    public void setAnswer(String answer) {
        Answer = answer;
    }
}

WorldHistory.java

public class WorldHistory extends AppCompatActivity {
    List<Question> questionList;
    int score = 0;
    int questionID = 0;

    Question currentQuestion;
    TextView txtQuestion;
    RadioButton rbtnA, rbtnB, rbtnC, rbtnD;
    Button btnNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_world_history);

        DatabaseHandler dba = new DatabaseHandler(this);
        questionList = dba.getAllQuestions();
        currentQuestion = questionList.get(questionID);

        txtQuestion = (TextView) findViewById(R.id.txtQuestion);
        rbtnA = (RadioButton) findViewById(R.id.rtbOptionA);
        rbtnB = (RadioButton) findViewById(R.id.rtbOptionB);
        rbtnC = (RadioButton) findViewById(R.id.rtbOptionC);
        rbtnD = (RadioButton) findViewById(R.id.rtbOptionD);
        btnNext = (Button) findViewById(R.id.btnNext);

        setQuestionView();
    }

    private void setQuestionView() {
        txtQuestion.setText(currentQuestion.getQuestion());
        rbtnA.setText(currentQuestion.getOptionA());
        rbtnB.setText(currentQuestion.getOptionB());
        rbtnC.setText(currentQuestion.getOptionC());
        rbtnD.setText(currentQuestion.getOptionD());
        questionID++;
    }
}     

以下是错误消息:

07-10 20:04:11.346 2514-2514/com.example.nasimahmed.quizgame E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.nasimahmed.quizgame, PID: 2514


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nasimahmed.quizgame/com.example.nasimahmed.quizgame.WorldHistory}: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference


                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
                                                                                   at com.example.nasimahmed.quizgame.DatabaseHandler.addQuestion(DatabaseHandler.java:81)
                                                                                   at com.example.nasimahmed.quizgame.DatabaseHandler.addQuestions(DatabaseHandler.java:61)
                                                                                   at com.example.nasimahmed.quizgame.DatabaseHandler.onCreate(DatabaseHandler.java:46)
                                                                                   at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                                   at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                                   at com.example.nasimahmed.quizgame.DatabaseHandler.getAllQuestions(DatabaseHandler.java:89)
                                                                                   at com.example.nasimahmed.quizgame.WorldHistory.onCreate(WorldHistory.java:29)
                                                                                   at android.app.Activity.performCreate(Activity.java:6237)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)

4 个答案:

答案 0 :(得分:2)

请改用此DatabaseHandler:

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "quiz";
    private static final String TABLE_NAME = "worldhistory";
    private static final String KEY_ID = "id";
    private static final String QUESTIONS = "question";
    private static final String ANSWER = "answer";
    private static final String OPTIONA = "optiona";
    private static final String OPTIONB = "optionb";
    private static final String OPTIONC = "optionc";
    private static final String OPTIOND = "optiond";

    private SQLiteDatabase dba;

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + QUESTIONS + " TEXT, "
                + ANSWER + " TEXT, " + OPTIONA + " TEXT, " + OPTIONB + " TEXT, " + OPTIONC
                + " TEXT, " + OPTIOND + " TEXT)";

        db.execSQL(CREATE_TABLE);

        addQuestions(db);
    }

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

    // creating the questions, options, and answers
    private void addQuestions(SQLiteDatabase db) {
        Question q1 = new Question("World War I began in which year?", "1923",
                "1938", "1917", "1914", "D");
        this.addQuestion(q1, db);

        Question q2 = new Question("Adolf Hitler was born in which country?",
                "France", "Germany", "Austria", "Hungary", "C");
        this.addQuestion(q2, db);

        //add more questions
    }

    // addding new questions to db
    public void addQuestion(Question question, SQLiteDatabase db) {
        ContentValues values = new ContentValues();
        values.put(QUESTIONS, question.getQuestion());
        values.put(ANSWER, question.getAnswer());
        values.put(OPTIONA, question.getOptionA());
        values.put(OPTIONB, question.getOptionB());
        values.put(OPTIONC, question.getOptionC());
        values.put(OPTIOND, question.getOptionD());

        db.insert(TABLE_NAME, null, values);
    }

    public List<Question> getAllQuestions() {
        List<Question> questionList = new ArrayList<Question>();

        String query = "SELECT * FROM " + TABLE_NAME;
        dba = this.getReadableDatabase();
        Cursor cursor = dba.rawQuery(query, 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.setOptionA(cursor.getString(3));
                quest.setOptionB(cursor.getString(4));
                quest.setOptionC(cursor.getString(5));
                quest.setOptionD(cursor.getString(6));

                questionList.add(quest);

            } while (cursor.moveToNext());
        }

        cursor.close();
        dba.close();
        return questionList;
    }

    public int rowCount() {
        int row = 0;
        String query = "SELECT * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        row = cursor.getCount();

        cursor.close();
        return row;
    }
}

答案 1 :(得分:1)

按以下方式更改插入方法,

   public void addQuestion(Question question)  
   {
        dba = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(QUESTIONS, question.getQuestion());
        values.put(ANSWER, question.getAnswer());
        values.put(OPTIONA, question.getOptionA());
        values.put(OPTIONB, question.getOptionB());
        values.put(OPTIONC, question.getOptionC());
        values.put(OPTIOND, question.getOptionD());

        dba.insert(TABLE_NAME, null, values);
   }

答案 2 :(得分:0)

dba 返回null,因为您使用 db 创建数据库。所以请改用 db.insert

答案 3 :(得分:0)

我认为这可能是因为 dba 之前从未进行过初始化。 尝试从主要活动(WorldHistory.java)添加问题。 像这样:

{{1}}

然后使用此 db 来调用 insert()方法

更多信息:https://developer.android.com/training/basics/data-storage/databases.html#WriteDbRow