ORMLite를 사용하여 내 앱을 내 앱에있는 SqliteDatabase
에 저장합니다.OrmLite java.lang.IllegalStateException : dao를 사용하기 전에 initialize()를 호출해야합니다.
개체를 유지하기 위해 DAO를 가져 오는 동안이 예외가 발생합니다.
문서는 나는 내가 DAO를 사용하기 전에 initialize()
메소드를 호출 할 필요가 말한다, 그리고 (부족)을 OrmLite 문서는 말한다 :
BaseDaoImpl (클래스)
초기화() 다음 초기화 다양한 세터가 호출 된 후 다양한 DAO 구성.
문제는 내가 getDao(class)
를 호출하여 DAO를 얻을이다, 나는의 DAO에도 OrmLiteSqliteOpenHelper
를 확장 내 수업도 호출 할 수있는 initialize()
이 없습니다.
이
내 사용자 정의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());
}
}
그리고 이것은 안드로이드 모니터의 전체 스택입니다 :
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()
방법 코드 여기 SiacApplication
라는 Application
서브 클래스를 추가 (매니페스트에 등록됨) :
@Override
public void onCreate() {
super.onCreate();
LocalDBHelper.getInstance(this);
}
,515,
EDIT 2 : 추가 DAO 게터 (DAO를 로컬 필드) 안드로이드 동일한 문제에 직면
public Dao<Barrio, Long> getBarrioDao() throws SQLException {
if (barrioDao == null) barrioDao = getDao(Barrio.class);
return barrioDao;
}
:
그래서 내가처럼 보였다 LocalDBHelper에서 DAO에 대한 래퍼게터를 만들었습니다. –
변경 사항을 편집했습니다. 여전히 동일한 예외가 발생합니다. – sbd2
DAO 인스턴스에 문제가 발생했습니다. 도우미 한테서 가져 왔어? DAO를 만드는 실제 코드를 보여 주시겠습니까? – Gray