gpt4 book ai didi

java - Android SQLite 测验应用程序

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:53 25 4
gpt4 key购买 nike

<分区>

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

数据库处理器.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;
}
}

问题.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;
}
}

世界历史.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)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com