1

我正在尝试访问 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;
4

3 回答 3

3

Context你传递给你的 sqlite 助手的是null.

于 2014-03-18T10:24:22.037 回答
0

_c 它没有被初始化... :( 抱歉。我在类活动的构造函数中调用了一个 Context 上下文,在里面我写了 this._c = context

于 2014-03-18T10:58:13.313 回答
0

您应该按照代码调用 createAllProductCategory(list_categories) 函数。DatabaseManager dbmanager = new DatabaseManager(_c); dbmanager.open(); dbmanager.createAllProductCategory(list_categories); dbmanager.close();

于 2014-03-18T10:34:12.320 回答