gpt4 book ai didi

Android NullPointerException + GetDatabaseLocked

转载 作者:太空狗 更新时间:2023-10-29 15:30:05 26 4
gpt4 key购买 nike

我的程序有问题。我使用数据库来保存我的应用程序的设置。当我在应用程序中保存密码时一切正常,但是当我使用原则上相同的方法保存“学校类(class)”的名称时,但另一个表崩溃了,我遇到了这个错误:

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

最佳答案

您传递给 SQLiteOpenHelper 构造函数的 Contextnull

关于Android NullPointerException + GetDatabaseLocked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21883777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com