2

我正在使用 RoboGuice 2.0 和 ormlite 开发一个 android 项目 v15。

我已经通过使用业务层和道层实现了数据访问。每个 Activity / Service 调用业务层也处理 Daos 之间的事务。

在我的第一次尝试中,一切正常,在我的 Daos 中,我有:

@Inject protected static DatabaseProvider databaseProvider;

因此,在 roboguice 模块配置中,我为需要提供程序的相关 dao 类执行 requestStaticInjection。

我选择从 databaseProvider 中删除 static 关键字,并从模块配置中删除 requestStaticInjection。

一切似乎都很好,应用程序启动并可以访问数据库。当 Activity 上下文调用 Dao 时,一切正常,但是当从 Service Context 调用 Dao 时,我得到了这个异常:

08-22 09:11:12.735: E/IncaricoServiceImpl(1779): Caused by: com.google.inject.ProvisionException: Guice provision errors:
08-22 09:11:12.735: E/IncaricoServiceImpl(1779): 1) Error in custom provider, java.util.EmptyStackException
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):   at roboguice.config.DefaultRoboModule.configure(DefaultRoboModule.java:130)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):   while locating android.content.Context
08-22 09:11:12.735: E/IncaricoServiceImpl(1779): 1 error
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at it.cpmapave.fgas.ioc.provider.DatabaseProvider.get(DatabaseProvider.java:18)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at it.cpmapave.fgas.dao.orm.GenericDaoOrm.getDao(GenericDaoOrm.java:32)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at it.cpmapave.fgas.dao.orm.GenericDaoOrm.queryForId(GenericDaoOrm.java:47)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at it.cpmapave.fgas.service.impl.IncaricoServiceImpl$1.call(IncaricoServiceImpl.java:103)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at it.cpmapave.fgas.service.impl.IncaricoServiceImpl$1.call(IncaricoServiceImpl.java:1)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at com.j256.ormlite.misc.TransactionManager.callInTransaction(TransactionManager.java:168)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    ... 8 more
08-22 09:11:12.735: E/IncaricoServiceImpl(1779): Caused by: java.util.EmptyStackException
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at java.util.Stack.peek(Stack.java:57)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at roboguice.inject.ContextScope$1.get(ContextScope.java:108)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
08-22 09:11:12.735: E/IncaricoServiceImpl(1779):    ... 14 more

这是我从 ormlite databaseProvider 获取 Dao 的方法:

protected Dao<T, Integer> getDao(){
    try {
        return databaseProvider.get().getDao(type);
    } catch (SQLException e) {
        if (LogConfig.ERROR_LOGS_ENABLED){
            Log.e(LOG_TAG, "Errore creazione Dao "+type.getSimpleName(), e);
        }
        return null;
    }
}

这里是我的 DatabaseProvider:

public class DatabaseProvider implements Provider<OrmLiteSqliteOpenHelper> {

    @Inject
    private Provider<Context> contextProvider;

    @Override
    public OrmLiteSqliteOpenHelper get() {
        return OpenHelperManager.getHelper(contextProvider.get().getApplicationContext(), DatabaseOpenHelper.class);
    }
}

正如所说的例外,似乎是一个上下文问题..

感谢您的任何建议!

马可

4

0 回答 0