带有SQlite数据库的Android游戏崩溃



我正在构建一个游戏应用程序,用户可以从 5 个类别中随机选择 1 个,然后问题将显示多个选项。我创建了一个数据库来保存问题和答案。有 5 个表格的问题谁、什么、何时、何地、为什么。

我检查了数据库浏览器并创建了"谁"类别。我只是想先尝试一下,看看我是否在正确的页面上。但是当类别加载时,应用程序崩溃。因此,在选择类别后,意向将转移到 QuestionActiviy 布局,在该布局中,它将在文本视图中加载问题,并让用户有机会选择答案。但是当我进入"问题活动"布局时,应用程序崩溃了。

这是数据库

public class MySQLiteHelper extends SQLiteOpenHelper {
    private static final String LOG = "MySQLiteHelper";
    //Database Version
    private static final int DATABASE_VERSION = 1;
    //Database Name
    private static final String DATABASE_NAME = "quizQuestions";
    //Question Tables
    public static final String TABLE_WHO = "who";
    //Common Column names
    private static final String KEY_ID="id";
    //Who Table Column
    private static final String KEY_WHO_QUESTION="who_question";
    private static final String KEY_WHO_MULTIPLE_CHOICEA="who_multiple_choiceA";
    private static final String KEY_WHO_MULTIPLE_CHOICEB="who_multiple_choiceB";
    private static final String KEY_WHO_MULTIPLE_CHOICEC="who_multiple_choiceC";
    private static final String KEY_WHO_MULTIPLE_CHOICED="who_multiple_choiceD";
    private static final String KEY_WHO_ANSWER="who_answer";
    //Create Who Table
    private static final String CREATE_TABLE_WHO = "CREATE TABLE "
            + TABLE_WHO  + "(" + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_WHO_QUESTION
            + " TEXT," + KEY_WHO_MULTIPLE_CHOICEA + "Text," + KEY_WHO_MULTIPLE_CHOICEB + "Text," +
             KEY_WHO_MULTIPLE_CHOICEC + "Text," +KEY_WHO_MULTIPLE_CHOICED + "Text," + KEY_WHO_ANSWER + "Text," + ")";
    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        //create tables
        //Question Tables
        db.execSQL(CREATE_TABLE_WHO);
    }
    public void onUpgrade(SQLiteDatabase db, int OldVersion, int NewVersion) {
           db.execSQL("DROP TABLE IF EXISTS " + TABLE_WHO);

       onCreate(db);
        }

    public List<QuestionsTable> getAllWhos() {
        List<QuestionsTable> whoQuestions = new ArrayList<QuestionsTable>();
        String selectQuery = "SELECT  * FROM " + TABLE_WHO;
        Log.e(LOG, selectQuery);
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                QuestionsTable allWhos = new QuestionsTable();
                allWhos.setId(c.getInt((c.getColumnIndex(KEY_ID))));
                allWhos.setQuestion((c.getString(c.getColumnIndex(KEY_WHO_QUESTION))));
                allWhos.setMultipleChoiceA(c.getString(c.getColumnIndex(KEY_WHO_MULTIPLE_CHOICEA)));
                allWhos.setMultipleChoiceB(c.getString(c.getColumnIndex(KEY_WHO_MULTIPLE_CHOICEB)));
                allWhos.setMultipleChoiceC(c.getString(c.getColumnIndex(KEY_WHO_MULTIPLE_CHOICEC)));
                allWhos.setMultipleChoiceD(c.getString(c.getColumnIndex(KEY_WHO_MULTIPLE_CHOICED)));
                allWhos.setAnswer(c.getString(c.getColumnIndex(KEY_WHO_ANSWER)));

                // adding to todo list
               whoQuestions.add(allWhos);
            } while (c.moveToNext());
        }
        return whoQuestions;
    }
 //Create a Who item in the questions table
    public void createWhoItem(QuestionsTable quests) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_ID, quests.getId());
        values.put(KEY_WHO_QUESTION, quests.getQuestion());
        values.put(KEY_WHO_MULTIPLE_CHOICEA, quests.getMultipleChoiceA());
        values.put(KEY_WHO_MULTIPLE_CHOICEB, quests.getMultipleChoiceB());
        values.put(KEY_WHO_MULTIPLE_CHOICEC, quests.getMultipleChoiceC());
        values.put(KEY_WHO_MULTIPLE_CHOICED, quests.getMultipleChoiceD());
        // insert row
        db.insert(TABLE_WHO, null, values);
    }
     //Who Questions
    public void addWhoQuestions() {
        QuestionsTable q1 = new QuestionsTable("Who was David's daughter?", "Tamar", "Macah", "Bathsheba",
                "Abigail", "Tamar");
        this.createWhoItem(q1);
        QuestionsTable q2 = new QuestionsTable("Who was Israel’s Firstborn son?", "Reuben", "Joseph", "Benjamin",
                "Judah", "Reuben");
        this.createWhoItem(q2);
        QuestionsTable q3 = new QuestionsTable("Who was the son of Jesse?", "Saul", "David", "Samson", "Goliath", "David");
        this.createWhoItem(q3);
        QuestionsTable q4 = new QuestionsTable("In Jesus' parable of the good Samaritan which of these passes by the injured man on the road?",
                "Priest", "Lawyer", "Servant", "Woman", "Servant");
        this.createWhoItem(q4);
        QuestionsTable q5 = new QuestionsTable("Who was not killed as part of satan's test in the book of Job, but for cursing God?",
                "Children", "Wife", "Friends", "Job", "Job");
        this.createWhoItem(q5);
        QuestionsTable q6 = new QuestionsTable("Who hired Delilah to cut Samson's hair?", "Philistines", "Israelites", "Agagites", "Amalekites", "Philistines");
        this.createWhoItem(q6);
        QuestionsTable q7 = new QuestionsTable("Who was known as the “weeping prophet?", "Ezekiel", "Jeremiah", "Isaiah", "Lamentations", "Jeremiah");
        this.createWhoItem(q7);
        QuestionsTable q8 = new QuestionsTable("Who was exiled on the isle of Patmos?", "John", "Paul", "Peter", "Onesimus", "John");
        this.createWhoItem(q8);
        QuestionsTable q9 = new QuestionsTable("Who was Moses father-in-law?", "Pharaoh", "Jethro", "Aaron", "Peter", "Jethro");
        this.createWhoItem(q9);
        QuestionsTable q10 = new QuestionsTable("Who did Solomon petition for supplies and workers for the temple?", "King of Tyre", "King of Syria", "King of Hittites", "King of Judah" , "King of Tyre");
        this.createWhoItem(q10);
    }
}//END MySQLiteHelper Class

下面是活动类:

public class QuestionActivity extends AppCompatActivity {
    List<QuestionsTable> quesList;
    int score=0;
    int qid=0;
    QuestionsTable currentQ;
    Button btnA, btnB, btnC, btnD;
    Button butSkip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
        MySQLiteHelper db=new MySQLiteHelper(this);
        quesList=db.getAllWhos();
        currentQ=quesList.get(qid);
        final TextView txtQuestion=(TextView)findViewById(R.id.txtViewQuestion);
        new CountDownTimer(60000, 1000) {
            public void onTick(long millisUntilFinished) {
                TextView textView = (TextView) findViewById(R.id.txtViewTimer);
                textView.setText(millisUntilFinished / 1000 + " seconds remaining");
                setQuestionView();
            }
            public void setQuestionView(){
                txtQuestion.setText(currentQ.getQuestion());
                btnA.setText(currentQ.getMultipleChoiceA());
                btnB.setText(currentQ.getMultipleChoiceB());
                btnC.setText(currentQ.getMultipleChoiceC());
                btnD.setText(currentQ.getMultipleChoiceD());
                qid++;
            }
            public void onClickQuestion(View view) {
                switch (view.getId()) {
                    case btnOptionA:
                        if (currentQ.getAnswer().equals(btnA.getText())) {
                            score++;
                            Log.d("score", "Your score" + score);
                        }
                        break;
                    case btnOptionB:
                        if (currentQ.getAnswer().equals(btnB.getText())) {
                            score++;
                            Log.d("score", "Your score" + score);
                        }
                        break;
                    case btnOptionC:
                        if (currentQ.getAnswer().equals(btnC.getText())) {
                            score++;
                            Log.d("score", "Your score" + score);
                        }
                        break;
                    case R.id.btnOptionD:
                        if (currentQ.getAnswer().equals(btnD.getText())) {
                            break;
                        }
                }
            }
            public void onFinish() {
                TextView textView = (TextView) findViewById(R.id.txtViewTimer);
                textView.setText("Done!");
                    Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
                    Bundle b = new Bundle();
                    b.putInt("score", score); //Your score
                    intent.putExtras(b); //Put your score to your next Intent
                    startActivity(intent);
                    finish();
                startActivity(intent);
            }
        }.start();
    }
}

这是来自Android Studio的日志猫

04-08 17:55:07.982 3741-3748/com.example.dan.biblechallenge I/art: Ignoring second debugger -- accepting and dropping
04-08 17:55:14.665 3741-3741/com.example.dan.biblechallenge I/Choreographer: Skipped 42 frames!  The application may be doing too much work on its main thread.
04-08 17:55:26.299 3741-3741/com.example.dan.biblechallenge E/MySQLiteHelper: SELECT  * FROM who
04-08 17:55:26.302 3741-3741/com.example.dan.biblechallenge E/SQLiteLog: (1) near ")": syntax error
04-08 17:55:26.302 3741-3741/com.example.dan.biblechallenge D/AndroidRuntime: Shutting down VM
04-08 17:55:26.303 3741-3741/com.example.dan.biblechallenge E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.dan.biblechallenge, PID: 3741
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dan.biblechallenge/com.example.dan.biblechallenge.QuestionActivity}: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE who(id INTEGER PRIMARY KEY, who_question TEXT,who_multiple_choiceAText,who_multiple_choiceBText,who_multiple_choiceCText,who_multiple_choiceDText,who_answerText,)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                               Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE who(id INTEGER PRIMARY KEY, who_question TEXT,who_multiple_choiceAText,who_multiple_choiceBText,who_multiple_choiceCText,who_multiple_choiceDText,who_answerText,)
                                                                                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
                                                                                  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
                                                                                  at com.example.dan.biblechallenge.MySQLiteHelper.onCreate(MySQLiteHelper.java:173)
                                                                                  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                                  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                                  at com.example.dan.biblechallenge.MySQLiteHelper.getAllWhos(MySQLiteHelper.java:216)
                                                                                  at com.example.dan.biblechallenge.QuestionActivity.onCreate(QuestionActivity.java:42)
                                                                                  at android.app.Activity.performCreate(Activity.java:6662)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
将表

创建查询替换为以下内容。

private static final String CREATE_TABLE_WHO = "CREATE TABLE "
        + TABLE_WHO  + "(" + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_WHO_QUESTION
        + " TEXT," + KEY_WHO_MULTIPLE_CHOICEA + "Text," + KEY_WHO_MULTIPLE_CHOICEB + "Text," +
         KEY_WHO_MULTIPLE_CHOICEC + "Text," +KEY_WHO_MULTIPLE_CHOICED + "Text," + KEY_WHO_ANSWER + "Text" + ")";

最新更新