我有Android服务,它使用SQLite数据库。我在数据库初始化方面遇到了一些麻烦。
我有DBHelper课程:
public class DBHelper extends SQLiteOpenHelper{
public boolean db_loaded=false;
public DBHelper() {
super(ContextBean.getLocalContext(), "resplugin1.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table tasks ("
+ "id integer primary key autoincrement,"
+ "title text);");
db_loaded=true;
}
我有使用它的客户端类:
public class DataProvider {
private SQLiteDatabase database;
private DBHelper dbHelper;
public DataProvider(){
dbHelper=new DBHelper();
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
public TaskDescriptor getTaskById(int id){
while(dbHelper.db_loaded==false){}
String[] args={Integer.toString(id)};
if(database==null){
Log.d("ResPluginService1","DATABASE IS NULL");
}
/*...*/
}
问题是数据库对象总是 NULL 。 getWritableDatabase 方法不会抛出异常,它只返回null。什么会导致如此奇怪的行为?