Android:getWriteableDatabase()错误

时间:2012-02-11 18:27:36

标签: android database sqlite nullpointerexception getwritabledatabase

我已经有这个问题已经有几天了,并且真的无法看到代码中的问题,所以真的很感激一些额外的目光。

我通过简单地创建一个新的数据库管理器对象并打开管理器数据库来启动应用程序。

 DBManager db_manager = new DBManager(this);
 db_manager.open()

应该应用应用程序执行简单的插入事务,但会抛出错误:

02-11 18:13:30.350: E/AndroidRuntime(8961): Caused by: java.lang.NullPointerException
02-11 18:13:30.350: E/AndroidRuntime(8961):     at com.test.DB.DBManager$DatabaseHelper.onCreate(DBManager.java:44)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at com.test.DB.DBManager.open(DBManager.java:61)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at com.test.DB.Display.onCreate(Display.java:26)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)

DatabaseHelper类:

    class DatabaseHelper extends SQLiteOpenHelper{

    DatabaseHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){            
            the_db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
}

DBManager方法open()导致问题。

    public DBManager(Context c){
    this.context = c;
    helper = new DatabaseHelper(context); 
}


// Makes the_db available to the Manager class.
public DBManager open() throws SQLException{
    the_db = helper.getWritableDatabase();
    return this;
}

1 个答案:

答案 0 :(得分:1)

尝试:

    @Override
    public void onCreate(SQLiteDatabase db){            
            db.execSQL(DATABASE_CREATE);
    }

您已经在onCreate()方法中获取了数据库,SQLiteDatabase参数是 db ,您必须使用它。

编辑: 您获得了NullPointerException,因为您在executeSQL()上调用了the_db,这是DBManager中的一个字段,可能声明为:

SQLiteDatabase the_db;

SQliteDatabase the_db = null;

所以当你实际进入onCreate(SQLiteDatabase db)时,你会在executeSQL()上调用null。 DBManager的代码应该是这样的:

public class DBManager {

    //the actual database you will use to insert,select etc after the getWritableDatabase() call
    private SQLiteDatabase the_db; 
    private Context context;
    private DatabaseHelper helper;

    public DBManager(Context c) {
        this.context = c;
        helper = new DatabaseHelper(context);
    }

    // Makes the_db available to the Manager class.
    public DBManager open() throws SQLException {
        the_db = helper.getWritableDatabase();
        return this;
    }

    class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, "something.db", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // the db is the SQLiteDatabase that android will offer you
            // and you must use it!
            db.execSQL("CREATE TABLE nametables(name TEXT);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }

}