我正在尝试从资产中复制数据库,但是在我的createDatabase()方法中,我得到一个空指针异常。这在调用getReadableDatabase时发生。
public static synchronized MetroLinkDatabaseHelper getInstance(Context context) {
// Using a singleton to minmize the chance of opening multiple
// decreasing any chance of memory leak
if(myInstance == null) {
myInstance = new MetroLinkDatabaseHelper(context);
}
return myInstance;
}
private MetroLinkDatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.mycontext = context;
boolean dbexist = checkdatabase();
if (dbexist) {
} else {
System.out.println("Database doesn't exist");
try {
createdatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// Create (copy the original in assets) if not exist using copydatabase
public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if (dbexist) {
} else {
// SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
this.getReadableDatabase();
try {
copydatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
以下是错误日志:
08-29 14:13:05.147 2222-2222/com.bkane56.practice.practiceapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bkane56.practice.practiceapp, PID: 2222
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.bkane56.practice.practiceapp/com.bkane56.practice.practiceapp.ListStopsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:268)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.bkane56.practice.practiceapp.MetroLinkDatabaseHelper.createdatabase(MetroLinkDatabaseHelper.java:66)
at com.bkane56.practice.practiceapp.MetroLinkDatabaseHelper.<init>(MetroLinkDatabaseHelper.java:52)
at com.bkane56.practice.practiceapp.MetroLinkDatabaseHelper.getInstance(MetroLinkDatabaseHelper.java:38)
at com.bkane56.practice.practiceapp.ListStopsActivity.<init>(ListStopsActivity.java:28)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1089)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
第66行=
this.getReadableDatabase();
有什么想法吗?我之前以更长的形式问过这个问题,但没有得到任何帮助。
由于
答案 0 :(得分:1)
你的问题在这里:
com.bkane56.practice.practiceapp.ListStopsActivity.<init>(ListStopsActivity.java:28)
您尝试在Activity
super.onCreate()
方法onCreate()
之前使用Activity
实例执行某些操作。一般来说,这不起作用,例如像这样的例外。
不是从初始值设定项中调用getInstance()
上的MetroLinkDatabaseHelper
,而是在super.onCreate()
方法中将该调用移至onCreate()
之后。