2

I'm developing an android project v15 with RoboGuice 2.0 and ormlite.

I've implemented data access by using a Business Layer and a Dao Layer. Each Activity / Service call business layer that handle also transactions between Daos.

In my first attempt all is working, and in my Daos I have:

@Inject protected static DatabaseProvider databaseProvider;

So in roboguice modules configuration I do requestStaticInjection for the relative dao class that require the provider.

I've opted for remove static keyword from databaseProvider and also to remove the requestStaticInjection from modules configurations.

All seems to be ok, the app start and can access db. When the Dao is called by an Activity context all works ok, but when the Dao is called from a Service Context I get this exception:

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

Here is the method where i get Dao from ormlite databaseProvider:

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;
    }
}

and here my DatabaseProvider:

public class DatabaseProvider implements Provider<OrmLiteSqliteOpenHelper> {

    @Inject
    private Provider<Context> contextProvider;

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

as states the exception, seems to be a context problem..

Thank you for any advice!

Marco

gipinani
  • 14,038
  • 12
  • 56
  • 85

0 Answers0