gpt4 book ai didi

Android SQLiteDatabase空指针异常

转载 作者:行者123 更新时间:2023-11-30 04:49:42 26 4
gpt4 key购买 nike

我刚刚开始为 Android 开发。到目前为止,一切进展顺利,但现在我在实现 SQLite 数据库时遇到了一些问题。我的代码基于 Google 的记事本示例(运行良好),但我不断收到空指针异常,这似乎与 getWritableDatabase() 方法有关。

有问题的类是 QuizDbAdapter:

package com.mypackage.myapp;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class QuizDbAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_QUESTION = "question";
public static final String KEY_ANSWER1 = "answer1";
public static final String KEY_ANSWER2 = "answer2";
public static final String KEY_ANSWER3 = "answer3";
public static final String KEY_ANSWER4 = "answer4";
public static final String KEY_CORRECT_ANSWER = "correct_answer";
public static final String KEY_IMAGE = "image";
public static final String KEY_CATEGORIES = "categories";
public static final String KEY_IMAGEQUESTION = "image_question";
public static final String KEY_EXPLANATION = "explanation";

private static final String TAG = "QuizDbAdapter";
private DatabaseHelper databaseHelper;
private SQLiteDatabase database;

private static final String DATABASE_CREATE =
"create table questions (_id integer primary key autoincrement, "
+ "question text not null, answer1 text not null, answer2 text not null,"
+ "answer3 text not null, answer4 text not null, correct_answer integer not null,"
+ "image text not null, categories text not null, image_question boolean not null,"
+ "explanation text not null);"
+ "create table roadsigns (_id integer primary key autoincrement, "
+ ");";

private static final String DATABASE_NAME = "mydb.sqlite";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_QUESTIONS = "questions";

private final Context context;

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context c) {
super(c, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}

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

public QuizDbAdapter(Context c) {
this.context = c;
}

public QuizDbAdapter open() throws SQLException {
databaseHelper = new DatabaseHelper(context);
database = databaseHelper.getWritableDatabase();
return this;
}

public void close() {
databaseHelper.close();
}

public Cursor fetchAllQuestions() {
return database.query(TABLE_QUESTIONS, new String[] { KEY_ROWID, KEY_QUESTION,
KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_CORRECT_ANSWER,
KEY_IMAGE, KEY_CATEGORIES, KEY_IMAGEQUESTION, KEY_EXPLANATION },
null, null, null, null, null);
}

public Cursor fetchQuestion(long rowId) throws SQLException {
Cursor cursor = database.query(true, TABLE_QUESTIONS, new String[] { KEY_ROWID,
KEY_QUESTION, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4,
KEY_CORRECT_ANSWER, KEY_IMAGE, KEY_CATEGORIES, KEY_IMAGEQUESTION,
KEY_EXPLANATION }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
return cursor;
}
}

每当我尝试使用

打开数据库时
QuizDbHelper databaseHelper = new QuizDbHelper(this);
databaseHelper.open(); // <-- X(

应用程序阻塞并提供空指针异常,指示对 open() 的调用。

堆栈跟踪:

10-06 23:24:02.104: ERROR/AndroidRuntime(369): FATAL EXCEPTION: main
10-06 23:24:02.104: ERROR/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage.myapp/com.mypackage.myapp.QuestionActivity}: java.lang.NullPointerException
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.os.Looper.loop(Looper.java:123)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at java.lang.reflect.Method.invoke(Method.java:521)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at dalvik.system.NativeStart.main(Native Method)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): Caused by: java.lang.NullPointerException
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at com.mypackage.myapp.Quiz.newQuiz(Quiz.java:88)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at com.mypackage.myapp.QuestionActivity.onCreate(QuestionActivity.java:21)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): ... 11 more

最佳答案

Quiz.newQuiz 是静态方法吗?如果是这种情况,this 不可用(因此是一个空指针)。

关于Android SQLiteDatabase空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876592/

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