Roboguice 2.0(Android版):POJO误注入(总是空) [英] Roboguice 2.0 (android): POJO injection error (always null)

查看:146
本文介绍了Roboguice 2.0(Android版):POJO误注入(总是空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基本POJO类:

public class BaseDao {
   public BaseDao() {
   }
   // ... 
}

我的扩展POJO类:

My extends POJO class:

public class KelvinDao extends BaseDao {
   public KelvinDao () {
      super();
   }
   // ... 
}

我想在这样的服务使用KelvinDao:

I want to use KelvinDao in a service like that:

public class HanKelvinHandler extends HttpRequestHandler {

   @Inject
   private KelvinDao mKelvinDao;

   public void treatGet() {
      mKelvinDao.blabla(); !!! mKelvinDao is always NULL
   }

这真的很简单,但它不工作:(

It's really simple but it doesn't work :(

感谢你们的帮助!

推荐答案

你是如何创建HanKelvinHandler?如果您是在RoboGuice类,如RoboActivity的子类中这样做,那么它应该只是工作。例如:

How are you creating HanKelvinHandler? If you're doing it within a subclass of a RoboGuice class, such as RoboActivity, then it should just work. Example:

public class MyActivity extends RoboActivity
{
    @Inject
    private HanKelvinHandler m_handler;
    [...]
}

否则(即,你的另一个POJO中创建它),你经常吉斯土地是的,我相信你会需要使用喷油器来获得它的一个实例。例如:

Otherwise (i.e., you're creating it within another POJO), you're in regular Guice land, and I believe you will need to use the injector to get an instance of it. Example:

public class MyClass
{
    public void doSomething()
    {
        Injector injector = Guice.createInjector( new YourGuiceBindings() );
        HanKelvinHandler handler = injector.getInstance( HanKelvinHandler.class );
        handler.treatGet(); // mKelvinDao should be good now
    }
}

如果您还没有看到使用注射器之前,或者你不知道把什么YourGuiceBindings(),那么你可能需要阅读以下内容:

If you haven't seen the use of the injector before, or you don't know what to put for YourGuiceBindings(), then you may need to read the following:

https://github.com/roboguice/roboguice/wiki/Simple-定制绑定

的https://$c$c.google.com/ p /谷歌 - 吉斯/维基/ GettingStarted

好像应该有办法做到这一点不使用注射器,但我不知道。

It seems like there should be a way to do this without using the injector, but I don't know.

这篇关于Roboguice 2.0(Android版):POJO误注入(总是空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆