android - RoboGuice:如何在我的自定义类中使用 RoboGuice 提供的注入(inject)?

标签 android dependency-injection android-contentresolver roboguice

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

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

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

    [...]
}

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

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

最佳答案

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

关于android - RoboGuice:如何在我的自定义类中使用 RoboGuice 提供的注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24624785/

相关文章:

design-patterns - 依赖注入(inject)和单例设计模式

java - 使用 ContentResolver 更新联系人组时更新不起作用

android - 如何通过单个查询从内容提供商检索所有联系人详细信息?

java - 覆盖时何时不调用 super() 方法?

java - 如何在 Android 中用手指删除图像

android - 尽管有权限,但写入外部存储的权限被拒绝

javascript - 使用 Angular1+ ES6 时, Controller 函数中的依赖注入(inject)未定义, Controller 作为类

zend-framework - 如何在 SOA 中使用依赖注入(inject)?

android - 无法创建 ViewModel 类的实例(无法启动 Activity ComponentInfo)

Android 4.4 (KitKat) 上的 Android Gallery 为 Intent.ACTION_GET_CONTENT 返回不同的 URI