0

我有一个 MunchaDbAdapter,它打开连接并将 SQLiteDatabase 初始化为 getWritableDatabase,如下所示:

public MunchaDbAdapter open() throws SQLException {
    mDbHelper = new MunchDbHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

现在在另一个班级我像这样打开了 MunchaDbAdapter

private MunchaDbAdapter mDbHelper;
private SQLiteDatabase mDb;

public long insert(Recipient recipient, String[] images){
        ContentValues initialValues = new ContentValues();
        initialValues.put(FLD_RECIPIENT_ID, Recipient.getFldRecipientId());
        initialValues.put(FLD_INFO, Recipient.getFldInfo());
        initialValues.put(FLD_LATITUDE, Recipient.getFldLatitude());
        initialValues.put(FLD_LONGITUDE, Recipient.getFldLongitude());
        for(int i=0;i<images.length;i++){
            if(images[i]!=null){
                initialValues.put("FLD_IMAGE_URL"+(i+1), images[i]);
            }
        }

        mDbHelper.open();
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

这是我的错误,mDb 为空,那么任何机构都可以提到它有什么问题?

4

1 回答 1

0

我能从代码中得到的只是你正在尝试这样做:

第一类:

public SQLiteDatabase open() throws SQLException {
    MunchDbHelper mDbHelper = new MunchDbHelper(mCtx);
    SQLiteDatabase mDb = mDbHelper.getWritableDatabase();
    return mDb;
}

第 2 类:

private MunchaDbAdapter mDbHelper;
private SQLiteDatabase mDb;

public long insert(Recipient recipient, String[] images){
        ContentValues initialValues = new ContentValues();
        initialValues.put(FLD_RECIPIENT_ID, Recipient.getFldRecipientId());
        initialValues.put(FLD_INFO, Recipient.getFldInfo());
        initialValues.put(FLD_LATITUDE, Recipient.getFldLatitude());
        initialValues.put(FLD_LONGITUDE, Recipient.getFldLongitude());
        for(int i=0;i<images.length;i++){
            if(images[i]!=null){
                initialValues.put("FLD_IMAGE_URL"+(i+1), images[i]);
            }
        }
        mDbHelper = new MunchDbHelper(getApplicationCOntext());
        mDb = mDbHelper.open();
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

当然,如果您详细说明您拥有的代码,我可以为您提供更好的帮助。

于 2013-01-01T18:03:13.397 回答