使用DataBase SQlite的测验

时间:2014-04-05 09:58:42

标签: java android database eclipse sqlite

我正在尝试制作一个包含至少100个问题的Android测验游戏 我在网上找到了一个使用DataBase存储问题和答案的教程 我下载了源代码并将DBHelper.java文件复制到我的项目中,并且每件事情都运行良好但是当我想添加一个新问题时我的应用程序在向数据库添加任何东西时都会崩溃。

package com.example.seenapp;

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 = "triviaQuiz";
// 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);        
    addQuestions();
    //db.close();
}
private void addQuestions()
{
    Question q1=new Question("What is JP?","Jalur Pesawat", "Jack sParrow", "Jasa Programmer", "Jasa Programmer");
    this.addQuestion(q1);
    Question q2=new Question("where the JP place?", "Monas, Jakarta", "Gelondong, Bangun Tapan, bantul", "Gelondong, Bangun Tapan, bandul", "Gelondong, Bangun Tapan, bantul");
    this.addQuestion(q2);
    Question q3=new Question("who is CEO of the JP?","Usman and Jack", "Jack and Rully","Rully and Usman", "Rully and Usman" );
    this.addQuestion(q3);
    Question q4=new Question("what do you know about JP?", "JP is programmer home", "JP also realigy home", "all answer is true","all answer is true");
    this.addQuestion(q4);
    Question q5=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
    this.addQuestion(q5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
    // Create tables again
    onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
    //SQLiteDatabase db = this.getWritableDatabase();
    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());
    // Inserting Row
    dbase.insert(TABLE_QUEST, null, values);        
}
public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase=this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    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;
}
}

它试图处理这段代码

Question q5=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
    this.addQuestion(q5);

并将数字更改为6但不起作用!! 谢谢

崩溃前的logcat:

04-05 06:15:24.311: E/SoundPool(378): error loading /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.321: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.321: E/SoundPool(378): error loading /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.321: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.331: E/SoundPool(378): error loading /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.331: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.331: E/SoundPool(378): error loading /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.331: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.341: E/SoundPool(378): error loading /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.341: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-05 06:15:24.341: E/SoundPool(378): error loading /system/media/audio/ui/KeypressStandard.ogg
04-05 06:15:24.351: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
04-05 06:15:24.351: E/SoundPool(378): error loading /system/media/audio/ui/KeypressSpacebar.ogg
04-05 06:15:24.351: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
04-05 06:15:24.351: E/SoundPool(378): error loading /system/media/audio/ui/KeypressDelete.ogg
04-05 06:15:24.351: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
04-05 06:15:24.361: E/SoundPool(378): error loading /system/media/audio/ui/KeypressReturn.ogg
04-05 06:15:24.361: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
04-05 06:15:24.361: E/SoundPool(378): error loading /system/media/audio/ui/KeypressInvalid.ogg
04-05 06:15:24.361: W/AudioService(378): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
04-05 06:15:24.361: W/AudioService(378): onLoadSoundEffects(), Error -1 while loading samples
04-05 06:15:24.411: D/AndroidRuntime(5083): Shutting down VM
04-05 06:15:24.411: W/dalvikvm(5083): threadid=1: thread exiting with uncaught exception (group=0xb1a43ba8)
04-05 06:15:24.431: E/AndroidRuntime(5083): FATAL EXCEPTION: main
04-05 06:15:24.431: E/AndroidRuntime(5083): Process: com.example.seenjeemapp, PID: 5083
04-05 06:15:24.431: E/AndroidRuntime(5083): java.lang.IllegalStateException: Could not execute method of the activity
04-05 06:15:24.431: E/AndroidRuntime(5083):     at android.view.View$1.onClick(View.java:3823)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at android.view.View.performClick(View.java:4438)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at android.view.View$PerformClick.run(View.java:18422)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at android.os.Handler.handleCallback(Handler.java:733)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at android.os.Looper.loop(Looper.java:136)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at java.lang.reflect.Method.invoke(Method.java:515)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at dalvik.system.NativeStart.main(Native Method)
04-05 06:15:24.431: E/AndroidRuntime(5083): Caused by: java.lang.reflect.InvocationTargetException
04-05 06:15:24.431: E/AndroidRuntime(5083):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at java.lang.reflect.Method.invoke(Method.java:515)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at android.view.View$1.onClick(View.java:3818)
04-05 06:15:24.431: E/AndroidRuntime(5083):     ... 11 more
04-05 06:15:24.431: E/AndroidRuntime(5083): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5
04-05 06:15:24.431: E/AndroidRuntime(5083):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at java.util.ArrayList.get(ArrayList.java:308)
04-05 06:15:24.431: E/AndroidRuntime(5083):     at com.example.seenjeemapp.PlayActivity.AnswerCButton(PlayActivity.java:256)
04-05 06:15:24.431: E/AndroidRuntime(5083):     ... 14 more
04-05 06:15:24.471: W/ActivityManager(378):   Force finishing activity com.example.seenjeemapp/.PlayActivity
04-05 06:15:25.041: W/ActivityManager(378): Activity pause timeout for ActivityRecord{b22b2f78 u0 com.example.seenjeemapp/.PlayActivity t70 f}

2 个答案:

答案 0 :(得分:0)

Caused by: java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5

那就是出了什么问题。

at java.util.ArrayList.get(ArrayList.java:308)
at com.example.seenjeemapp.PlayActivity.AnswerCButton(PlayActivity.java:256)

那就是。

您的C按钮(其事件处理程序)尝试从get(5)个5个元素中ArrayList

ArrayList s(与Java中的所有索引集合一样)从0开始索引,因此第五个元素(最后一个元素)在4下编入索引,右边。它是0,1,2,3,4。

这就是为什么你得到IndexOutOfBoundsException

导致错误的代码不在您发布的代码范围内,因此很难确定错误,但看起来负责处理C答案的代码(或许可能是其他任何答案)不知道你添加了问题6这个事实。你一定忽略了一些东西。

我强烈建议您学习使用Logcat,否则每当遇到严重错误时,您总会陷入黑暗。

这里可能对您有用的一些链接:

答案 1 :(得分:0)

根据你的logcat get方法,我们的arraylost正在进行中。如果你的数组包含n个项,则get方法只能与n-1一起使用。

我看不到使用此get方法的位置,因此您应该发布更多代码。

其他为什么你评论了这一行

  SQLiteDatabase db = this.getWritableDatabase();

如何在不打开数据库的情况下执行任何操作。

顺便说一下导致应用程序崩溃的错误不在此代码中

一些可能不一定会导致崩溃的提示,但您应该在正确的时间和正确的范围内练习关闭和打开数据库和游标。