【发布时间】:2015-03-13 21:13:10
【问题描述】:
我需要从RemoteViewFactory 类的数据库中加载数据,我正在使用 OrmLite。
我有这个Helper 类:
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "gfkksa.db";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Issue.class);
TableUtils.createTable(connectionSource, IssuePriority.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database.");
throw new RuntimeException(e);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, Issue.class, true);
TableUtils.dropTable(connectionSource, IssuePriority.class, true);
onCreate(db, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop databases.");
throw new RuntimeException(e);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
public HashMap<String, Dao> getDaoFactory() {
HashMap<String, Dao> hashFactory = new HashMap<String, Dao>();
try {
hashFactory.put("issue", getDao(Issue.class));
hashFactory.put("issuePriority", getDao(IssuePriority.class));
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hashFactory;
}
}
一切正常,在 Activity 类 (extends OrmLiteBaseActivity<DatabaseHelper>) 中,例如我正在从 DB 中获取数据列表,如下所示:
ArrayList items = (ArrayList) getHelper().getDaoFactory().get("issue").queryForAll();
但如果我在实现RemoteViewsService.RemoteViewsFactory(AppWidget 的ListView 的远程视图工厂)的课堂上做同样的事情,应用程序会崩溃并显示以下异常消息:
03-18 16:13:29.244: E/AndroidRuntime(28500): java.lang.RuntimeException: Unable to bind to service cz.testbrana.widget.WidgetService@41e34110 with Intent { dat=intent: cmp=cz.testbrana.ebranasystem/cz.testbrana.widget.WidgetService (has extras) }: java.lang.IllegalStateException: A call has not been made to onCreate() yet so the helper is null
如何在 AppWidget 的 RemoteViewFactory 中使用 OrmLite?
【问题讨论】:
标签: android sqlite ormlite android-appwidget remoteview