我试图访问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);
}
}
}
logcat的
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)
在另一项活动中,我称之为:
DatabaseManager dbmanager = new DatabaseManager(_c);
dbmanager.createAllProductCategory(list_categories);
班级里也存在这个:
Context _c;
答案 0 :(得分:3)
您传递给sqlite助手的Context
为null
。
答案 1 :(得分:0)
您应该遵循代码来调用createAllProductCategory(list_categories)函数。 DatabaseManager dbmanager = new DatabaseManager(_c); dbmanager.open(); dbmanager.createAllProductCategory(list_categories); dbmanager.close();
答案 2 :(得分:0)
_c它没有初始化...... :(对不起。我在类活动的构造函数中调用了一个Context上下文,在里面我写了这个._c = context