gpt4 book ai didi

android - Android 上的 SQLiteOpenHelper.getWriteableDatabase() 空指针异常

转载 作者:IT王子 更新时间:2023-10-29 06:20:47 27 4
gpt4 key购买 nike

我在 Android 中使用带有直接 SQL 的 SQLite 时运气不错,但这是我第一次将 DB 包装在 ContentProvider 中。在调用 getWritableDatabase()getReadableDatabase() 时,我不断收到空指针异常。这只是我在代码初始化时犯的一个愚蠢错误,还是存在更大的问题?

public class DatabaseProvider extends ContentProvider {
...
private DatabaseHelper databaseHelper;
private SQLiteDatabase db;
...
@Override
public boolean onCreate() {
databaseHelper = new DatabaseProvider.DatabaseHelper(getContext());
return (databaseHelper == null) ? false : true;
}
...
@Override
public Uri insert(Uri uri, ContentValues values) {
db = databaseHelper.getWritableDatabase(); // NULL POINTER EXCEPTION HERE
...
}
private static class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "cogsurv.db";
public static final int DATABASE_VERSION = 1;

public static final String[] TABLES = {
"people",
"travel_logs",
"travel_fixes",
"landmarks",
"landmark_visits",
"direction_distance_estimates"
};

// people._id does not AUTOINCREMENT, because it's set based on server's people.id
public static final String[] CREATE_TABLE_SQL = {
"CREATE TABLE people (_id INTEGER PRIMARY KEY," +
"server_id INTEGER," +
"name VARCHAR(255)," +
"email_address VARCHAR(255))",
"CREATE TABLE travel_logs (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"server_id INTEGER," +
"person_local_id INTEGER," +
"person_server_id INTEGER," +
"start DATE," +
"stop DATE," +
"type VARCHAR(15)," +
"uploaded VARCHAR(1))",
"CREATE TABLE travel_fixes (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"datetime DATE, " +
"latitude DOUBLE, " +
"longitude DOUBLE, " +
"altitude DOUBLE," +
"speed DOUBLE," +
"accuracy DOUBLE," +
"travel_mode VARCHAR(50), " +
"person_local_id INTEGER," +
"person_server_id INTEGER," +
"travel_log_local_id INTEGER," +
"travel_log_server_id INTEGER," +
"uploaded VARCHAR(1))",
"CREATE TABLE landmarks (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"server_id INTEGER," +
"name VARCHAR(150)," +
"latitude DOUBLE," +
"longitude DOUBLE," +
"person_local_id INTEGER," +
"person_server_id INTEGER," +
"uploaded VARCHAR(1))",
"CREATE TABLE landmark_visits (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"server_id INTEGER," +
"person_local_id INTEGER," +
"person_server_id INTEGER," +
"landmark_local_id INTEGER," +
"landmark_server_id INTEGER," +
"travel_log_local_id INTEGER," +
"travel_log_server_id INTEGER," +
"datetime DATE," +
"number_of_questions_asked INTEGER," +
"uploaded VARCHAR(1))",
"CREATE TABLE direction_distance_estimates (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"server_id INTEGER," +
"person_local_id INTEGER," +
"person_server_id INTEGER," +
"travel_log_local_id INTEGER," +
"travel_log_server_id INTEGER," +
"landmark_visit_local_id INTEGER," +
"landmark_visit_server_id INTEGER," +
"start_landmark_local_id INTEGER," +
"start_landmark_server_id INTEGER," +
"target_landmark_local_id INTEGER," +
"target_landmark_server_id INTEGER," +
"datetime DATE," +
"direction_estimate DOUBLE," +
"distance_estimate DOUBLE," +
"uploaded VARCHAR(1))"
};

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.v(Constants.TAG, "DatabaseHelper()");
}

@Override
public void onCreate(SQLiteDatabase db) {
Log.v(Constants.TAG, "DatabaseHelper.onCreate() starting");

// create the tables
int length = CREATE_TABLE_SQL.length;
for (int i = 0; i < length; i++) {
db.execSQL(CREATE_TABLE_SQL[i]);
}
Log.v(Constants.TAG, "DatabaseHelper.onCreate() finished");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for (String tableName : TABLES) {
db.execSQL("DROP TABLE IF EXISTS" + tableName);
}
onCreate(db);
}
}
}

一如既往,感谢您的帮助!

--

不确定这个细节是否有帮助,但这是显示异常的 LogCat:

删除了无效的 ImageShack 链接

最佳答案

而不是:

databaseHelper = new DatabaseProvider.DatabaseHelper(getContext());

尝试:

databaseHelper = new DatabaseProvider.DatabaseHelper(this);

看看是否有帮助。

如果没有,您可以发布 NullPointerException 堆栈跟踪吗?

关于android - Android 上的 SQLiteOpenHelper.getWriteableDatabase() 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1741334/

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