SQLite getReadableDatabase() returns NULL

后端 未结 1 685
太阳男子
太阳男子 2021-01-28 13:10

I\'m using Android\'s SQLite to create a database of levels, based on some files. I first create a SQLiteOpenHelper, and on it I call getReadableDatabase() or getWritableDatabas

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 14:00

    Why are accessing an instance variable from an activity from inside your open helper? If I understand your code correctly, your loadLevelData() method is a method of your open helper. Instead of what you have, why not this:

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_TABLE_CREATE);
        loadLevelData(db);
    }
    
    // Load data into the database on creation
    private void loadLevelData(SQLiteDatabase db) {
        ...
        db.insert(DB_TABLE_NAME, null, info);
    } 
    

    The root of your problem is that the database instance variable in your activity isn't assigned by the time you access it down in your DbOpenHelper... you're still in the call that's creating the database -- which happens to be dbHelper.getWritableDatabase() -- and the result hasn't returned for the assignment.

    0 讨论(0)
提交回复
热议问题