2

我的程序有問題。 我使用數據庫來保存我的應用程序的設置。當我將密碼保存在應用程序中時,它一切正常,但是當我原則上使用相同的方法保存「學校課程」的名稱時,但另一個表格會崩潰,並且出現此錯誤:Android NullPointerException + GetDatabaseLocked

02-19 15:32:04.070: E/AndroidRuntime(14349): FATAL EXCEPTION: main 
02-19 15:32:04.070: E/AndroidRuntime(14349): java.lang.NullPointerException 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at de.vertretungsplan2.helper.DBHandler.insertKurs(DBHandler.java:95) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at de.vertretungsplan2.tabswipe.adapter.KurseFragment$1.onItemClick(KurseFragment.java:38) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.widget.AdapterView.performItemClick(AdapterView.java:297) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.widget.AbsListView.performItemClick(AbsListView.java:1100) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.widget.AbsListView$1.run(AbsListView.java:3463) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.os.Handler.handleCallback(Handler.java:730) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.os.Looper.loop(Looper.java:137) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at android.app.ActivityThread.main(ActivityThread.java:5289) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at java.lang.reflect.Method.invoke(Method.java:525) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 
02-19 15:32:04.070: E/AndroidRuntime(14349): at dalvik.system.NativeStart.main(Native Method) 

這是應該在數據庫中寫入方法:

public void insertKurs(String Kurs){ 
    long rowId = -1; 
    try{ 
     //open Database 
     SQLiteDatabase db = this.getWritableDatabase(); 
     //Content 
     ContentValues values = new ContentValues(); 
     values.put(KURS_NAME, Kurs); 
     //insert Values in Table 
     rowId = db.insert(TABLE_NAME_KURS, null, values); 
     db.close(); 
    }catch (SQLiteException e){ 
     Log.e(TAG,"insert()",e); 
    }finally{ 
     Log.d(TAG, "insert(): rowId=" + rowId);   
    } 
} 

此方法的工作沒有任何問題:

public void insertPW(String PW){ 
    SQLiteDatabase db = null; 
    long rowId = -1; 
    try{ 
     //open Database 
     db = this.getWritableDatabase(); 
     //Content 
     ContentValues values = new ContentValues(); 
     values.put(PASSWORD_NAME, PW); 
     //insert Values in Table 
     rowId = db.insert(TABLE_NAME_PW, null, values); 
     db.close(); 
    }catch (SQLiteException e){ 
     Log.e(TAG,"insert()",e); 
    }finally{ 
     Log.d(TAG, "insert(): rowId=" + rowId);   
    } 
} 

這是調用其他方法:

public void onItemClick(AdapterView<?>arg0,View v, int position,long id){     
      db.insertKurs(kursView1.getItemAtPosition(position).toString()); 

     } 
    }); 

謝謝你的幫助,對不起我的英語:d

+0

哪一行是'DBHandler.java'的第95行? –

回答

21

Context你傳遞給SQLiteOpenHelper構造是null

+0

好吧,但爲什麼它使用密碼方法? – user3328547

+0

private DBHandler db = new DBHandler(getActivity()); 那麼這應該是問題? – user3328547

+0

現在它的作品謝謝 – user3328547

相關問題