我正在扩展sqliteOpenHelper类.我的构造函数是
public MyDatabaseHelper(Context context) {
super(
context,// ???
"MyDatabase.db",// Database name
null,// Cursor factory
1 // database version
);
}
sqliteOpenHelper构造函数对上下文信息做了什么?
对于我的应用程序,无论程序状态(上下文)如何,构造函数的行为都相同.我可以在没有任何未来问题的情况下传递null吗?
解决方法
如果您提供空值,它将创建一个内存数据库,但您需要为数据库名称参数提供null,以便正确处理它.
这在上下文的构造函数文档中有记录
context to use to open or create the database name of the database
file,or null for an in-memory database
此外,如果您查看sqliteHelper类本身的源代码,您将看到它使用mName值来决定是否使用mContext.在线查看源代码:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r1.2/android/database/sqlite/SQLiteOpenHelper.java#SQLiteOpenHelper.0mContext