我正在使用ORMLite在我的應用中將我的對象保存在SqliteDatabase
中。OrmLite java.lang.IllegalStateException:您必須先調用initialize()才能使用dao
我得到這個異常,同時試圖獲得一個DAO爲了堅持一個對象。
文檔說我必須調用initialize()
方法之前,我可以使用DAO和OrmLite文檔(不足)說:
BaseDaoImpl(類)
初始化():初始化調用各種設置程序之後的各種DAO配置。
的問題是,我通過調用getDao(class)
得到的DAO,並沒有initialize()
,我可以打電話既不在DAO中,也不在我的類,它擴展OrmLiteSqliteOpenHelper
。
這是我的自定義OpenHelper
類代碼:
public class LocalDBHelper extends OrmLiteSqliteOpenHelper {
private LocalDBHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static LocalDBHelper getInstance(Context context) {
if (instance == null) instance = new LocalDBHelper(context);
return instance;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Barrio.class);
TableUtils.createTable(connectionSource, Fenomeno.class);
TableUtils.createTable(connectionSource, Info.class);
TableUtils.createTable(connectionSource, TelefonoUtil.class);
TableUtils.createTable(connectionSource, Alarma.class);
TableUtils.createTable(connectionSource, ReplicaAlerta.class);
} catch (SQLException e) {
e.printStackTrace();
Log.d(TAG, e.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, Barrio.class, true);
TableUtils.dropTable(connectionSource, Fenomeno.class, true);
TableUtils.dropTable(connectionSource, Info.class, true);
TableUtils.dropTable(connectionSource, TelefonoUtil.class, true);
TableUtils.dropTable(connectionSource, Alarma.class, true);
TableUtils.dropTable(connectionSource, ReplicaAlerta.class, true);
} catch (SQLException e) {
e.printStackTrace();
Log.d(TAG, e.getMessage());
}
}
這是Android的顯示器的完整的堆棧:
java.lang.IllegalStateException: you must call initialize() before you can use the dao
at com.j256.ormlite.dao.BaseDaoImpl.checkForInitialized(BaseDaoImpl.java:1061)
at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:316)
at com.org.siam.app.controller.BarrioController.actualizarTodos(BarrioController.java:75)
at com.org.siam.app.remote.BarriosWebService.onResponse(BarriosWebService.java:43)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
編輯:onCreate()
方法代碼在這裏增加了一個Application
子類,稱爲SiacApplication
,(還在艙單上註冊):
@Override
public void onCreate() {
super.onCreate();
LocalDBHelper.getInstance(this);
}
編輯2:加入DAO吸氣劑(在DAO是本地字段):
public Dao<Barrio, Long> getBarrioDao() throws SQLException {
if (barrioDao == null) barrioDao = getDao(Barrio.class);
return barrioDao;
}
我覺得你的applicationContext總是空,因此它認爲它不是initalized。 –
我已經編輯了我的更改。仍然得到相同的例外。 – sbd2
你的DAO實例有問題。你確定你是從幫手那裏得到的?你能否展示構建dao的實際代碼? – Gray