gpt4 book ai didi

android - 为什么我得到 getDatabaseLocked?

转载 作者:行者123 更新时间:2023-11-29 17:51:41 25 4
gpt4 key购买 nike

我正在尝试访问 sqlite 数据库,但我不知道我的代码做错了什么。我不知道问题出在哪里。你能看看吗?

public class DatabaseManager {

private SQLiteDatabase db;
private DatabaseHelper dbHelper;
private Context _context;
public int version_old = 0; //DB

public DatabaseManager(Context context) {
dbHelper = new DatabaseHelper(context);
}

public void open() throws SQLException {
db = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
db.close();
}

public boolean isOpen() {
return db.isOpen();
}

// ------------------------ "PRODUCTCATEGORY" table methods ----------------//

/*
* Creating a productcategory
*/
public long createProductCategory(Category cat){
open();

ContentValues values = new ContentValues();
values.put(dbHelper.KEY_ID_PCAT, cat.getId());
values.put(dbHelper.KEY_NAME_PCAT, cat.getName());
values.put(dbHelper.KEY_LATEST_UPDATE_DATE_PCAT, cat.getCategoryDate().getDate());

long returned_id = 0;
if (db != null) {
returned_id = db.insert(dbHelper.TABLE_PRODUCTCATEGORY, null, values);
}

if(isOpen()){
close();
}

return returned_id;
}

/**
* Creating all productcategories
*/

public void createAllProductCategory(ArrayList<Category> categories){

for(Category category : categories){
createProductCategory(category);
}

}


}

洛格卡特

03-18 11:15:01.025  16198-16198/com.testingsqli E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java)
at com.testingsqli.helper.DatabaseManager.createProductCategory(DatabaseManager.java:51)
at com.testingsqli.helper.DatabaseManager.createAllProductCategory(DatabaseManager.java:77)
at com.testingsqli.listeners.ProductListener.onRequestSuccess(ProductListener.java:59)
at com.testingsqli.listeners.ProductListener.onRequestSuccess(ProductListener.java:25)
at com.octo.android.robospice.request.notifier.DefaultRequestListenerNotifier$ResultRunnable.run(DefaultRequestListenerNotifier.java:174)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)

在另一个 Activity 中我称之为:

DatabaseManager dbmanager = new DatabaseManager(_c);
dbmanager.createAllProductCategory(list_categories);

类(class)中也存在这个:

Context _c;

最佳答案

您传递给 sqlite 助手的 Contextnull

关于android - 为什么我得到 getDatabaseLocked?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22476449/

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