1

我这几天一直有这个问题,并且真的看不到代码中的问题,因此非常感谢您多看几眼。

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

 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)

数据库助手类:

    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;
}
4

1 回答 1

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) {
        }
    }

}
于 2012-02-11T18:33:17.813 回答