【问题标题】:RoboGuice: how to use RoboGuice-provided injections in my custom class?RoboGuice:如何在我的自定义类中使用 RoboGuice 提供的注入?
【发布时间】:2014-07-08 06:09:14
【问题描述】:

我创建了一个依赖于 ContentResolver 的类:

public class MyClass
{
    // these Injects won't work
    @Inject
    private ContentResolver m_contentResolver;

    @Inject
    public MyClass( ContentResolver resolver )
    {
        m_contentResolver = resolver;
    }

    [...]
}

ContentResolver 是 RoboGuice 提供的注入 (https://github.com/roboguice/roboguice/wiki/RoboGuice-Standard-Injections),但这假设我在扩展 RoboGuice 类(如 RoboActivity)的类中使用注入。

有没有办法在我的自定义类中使用 RoboGuice 提供的注入?

【问题讨论】:

    标签: android dependency-injection android-contentresolver roboguice


    【解决方案1】:

    将此行添加到 MyClass 的构造函数中:RoboGuice.getInjector(myApplicationContext).injectMembers(this)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多