2015-10-15 25 views
0

我試圖讓數據庫輔助類的這樣的構造數據庫路徑:哪裏得到數據庫路徑在數據庫打開助手?

public DB_Nabege_helper(Context context) { 
    super(context, DBname, null, 1); 
    Nabege_context = context; 
    DBpath=Nabege_context.getDatabasePath(DBname).getPath(); 
} 

當運行應用程序:崩潰的應用程序

10-15 16:41:37.164: E/AndroidRuntime(1420): FATAL EXCEPTION: main 
10-15 16:41:37.164: E/AndroidRuntime(1420): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nabproduct.nabege/com.nabproduct.nabege.Collection_List_Activity}: java.lang.NullPointerException 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.os.Looper.loop(Looper.java:137) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at dalvik.system.NativeStart.main(Native Method) 
10-15 16:41:37.164: E/AndroidRuntime(1420): Caused by: java.lang.NullPointerException 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.content.ContextWrapper.getDatabasePath(ContextWrapper.java:231) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at com.nabproduct.nabege.DB_Nabege_helper.<init>(DB_Nabege_helper.java:44) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at com.nabproduct.nabege.Collection_List_Activity.<init>(Collection_List_Activity.java:165) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at java.lang.Class.newInstanceImpl(Native Method) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at java.lang.Class.newInstance(Class.java:1319) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053) 
10-15 16:41:37.164: E/AndroidRuntime(1420):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974) 

我添加更多的代碼作爲一個答案。

+0

attach logcat詳細信息 – USKMobility

+1

附加數據庫幫助類也 – USKMobility

+0

你怎麼稱呼它? –

回答

3

我從堆棧跟蹤中看到,您可能在活動構造函數或字段初始化程序中創建了DB助手。在這兩種情況下,活動尚未初始化(此外,在大多數情況下,您不需要重寫活動構造函數)。在活動onCreate()方法中移動DB幫助器分配。

+0

我想在DB助手類中獲取並分配數據庫路徑 – programmer138200

+0

@ programmer138200問題不在數據庫路徑中,問題出在錯誤的上下文中。 –

+0

我在我的活動的oncreate方法中使用此代碼:------ >>> nabege_db = new DB_Nabege_helper(this); ----它工作。謝謝你的回答 – programmer138200