我从DatabaseHandler获取NullPointerException。
这是我的DatabaseHandler:
public class DatabaseHandler extends SQLiteAssetHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "carsGrid",
TABLE_CARS = "cars ",
KEY_ID = "id",
KEY_POSTCODE = "postcode",
KEY_ADDRESS = "address",
KEY_IMAGE = "image";
public DatabaseHandler(Context context) {
//super(context, DATABASE_NAME, null, DATABASE_VERSION);
super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
}
}
MyActivity.java:
报告错误的行:
// Initiate Database
DatabaseHandler db = new DatabaseHandler(this); // "this" refer to the context
我感觉上下文导致问题,但我不确定上下文应该是什么。我刚从SQLiteOpenHelper
更改了上一行有错误:
public DatabaseHandler(Context context) {
//super(context, DATABASE_NAME, null, DATABASE_VERSION);
super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
}
答案 0 :(得分:1)
在Activity
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
或
DatabaseHandler db = new DatabaseHandler(YourActivity.this);