我正在尝试在我的Android应用程序上写入本地SQLlite数据库。 我在处理程序中创建了一个打开数据库的方法,但它正在崩溃应用程序。 我知道从使用日志开始就是这条线:
db = helper.getWritableDatabase();
这导致了崩溃,但我不明白为什么。
以下是处理程序类:
public static final String TAG = "TableSubandObAssessment";
public static final String KEY_TABLE = "assessments";
private static final String KEY_APPOINTMENT_ID = "appointmentID";
private static final String KEY_TYPE = "type";
private static final String KEY_ASSESSMENT = "assessment";
private static final String KEY_VARIABLE1 = "variable1";
private static final String KEY_VARIABLE2 = "vriable2";
private static final String KEY_VARIABLE3 = "vriable3";
private static final String KEY_VARIABLE4 = "vriable4";
private static DatabaseHelper helper;
private static Context context;
public static SQLiteDatabase db;
static public void createTable(SQLiteDatabase db) {
String createTbl =
"CREATE TABLE " + KEY_TABLE
+ " (" + KEY_APPOINTMENT_ID + " INTEGER PRIMARY KEY NOT NULL, "
+ KEY_TYPE + " TEXT NOT NULL, "
+ KEY_ASSESSMENT + " TEXT NOT NULL, "
+ KEY_VARIABLE1 + " TEXT, "
+ KEY_VARIABLE2 + " TEXT NOT NULL, "
+ KEY_VARIABLE3 + " TEXT, "
+ KEY_VARIABLE4 + " TEXT " + ");";
db.execSQL(createTbl);
}
public static void dropTable(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + KEY_TABLE);
createTable(db);
}
public TableSubandObAssessment(Context context) {
this.context = context;
}
public static SQLiteDatabase open() throws SQLException {
helper = new DatabaseHelper(context);
db = helper.getWritableDatabase();
return db;
}
public static void close() {
helper.close();
}
public static long insertTableSubandObAssessment(int appointmentID, String type, String assessment, String variable1, String variable2, String variable3, String variable4) {
ContentValues values = new ContentValues();
values.put(KEY_APPOINTMENT_ID, appointmentID);
values.put(KEY_TYPE, type);
values.put(KEY_ASSESSMENT, assessment);
values.put(KEY_VARIABLE1, variable1);
values.put(KEY_VARIABLE2, variable2);
values.put(KEY_VARIABLE3, variable3);
values.put(KEY_VARIABLE4, variable4);
return db.insert(KEY_TABLE, null, values);
}
这里是logcat:
08-07 14:34:55.806: E/AndroidRuntime(1995): FATAL EXCEPTION: main
08-07 14:34:55.806: E/AndroidRuntime(1995): java.lang.NullPointerException
08-07 14:34:55.806: E/AndroidRuntime(1995): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
08-07 14:34:55.806: E/AndroidRuntime(1995): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
08-07 14:34:55.806: E/AndroidRuntime(1995): at com.ucl.pga.db.TableSubandObAssessment.open(TableSubandObAssessment.java:58)
08-07 14:34:55.806: E/AndroidRuntime(1995): at com.ucl.pga.db.Objective.Summary.saveToDb(Summary.java:965)
08-07 14:34:55.806: E/AndroidRuntime(1995): at com.ucl.pga.db.Objective.Summary$2$1.onClick(Summary.java:480)
08-07 14:34:55.806: E/AndroidRuntime(1995): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-07 14:34:55.806: E/AndroidRuntime(1995): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 14:34:55.806: E/AndroidRuntime(1995): at android.os.Looper.loop(Looper.java:137)
08-07 14:34:55.806: E/AndroidRuntime(1995): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-07 14:34:55.806: E/AndroidRuntime(1995): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 14:34:55.806: E/AndroidRuntime(1995): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 14:34:55.806: E/AndroidRuntime(1995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-07 14:34:55.806: E/AndroidRuntime(1995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-07 14:34:55.806: E/AndroidRuntime(1995): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您传递给Context
构造函数的SQLiteOpenHelper
为null
。