2012-08-17 75 views
3

我是Roboguice的新手,我想在我的新Android应用程序中使用它。RoboGuice 2.0中的注入字段为null

我有一个扩展RoboActivity的测试Activity。

public class MainActivity extends RoboActivity { 
    @Inject 
    private TestService testService; 

    ....   
} 

这里是我的TestService的类:

public class TestService { 

    @Inject 
    private TestDao testDao; 

    @Inject 
    protected static Provider<Context> contextProvider; 

    public TestService(){ 
     Log.d("TEST_SERVICE", "Constructor test"); 
    } 

    public Test getById(Integer id) throws Exception{ 
     return testDao.queryForId(id); 
    } 
} 

我希望在@Injected注解的字段内注射类将被注入!

TestService由MainActivity注入。但TestDao是空的,也是我的contextProvider!

我也定义定义我IoCModule类roboguice.xml文件:

public class IoCModule extends AbstractModule{ 
    @Override 
    protected void configure() { 
     bind(TestDao.class).to(TestDaoOrm.class); 
    } 
} 

我不知道为什么内@Inject将无法正常工作!

谢谢你的任何建议!

谢谢 马尔科

回答

2

我已经解决了把我的模块定义

requestStaticInjection(TestDaoOrm.class);