尚未注入Android Dagger 2依赖关系 [英] Android Dagger 2 Dependency not being injected

查看:97
本文介绍了尚未注入Android Dagger 2依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中使用Dagger 2,但是我在实体存储库方面遇到了一些问题,而且我还没有弄清我所缺少的内容。

i'm trying to use Dagger 2 into my apps but i'm having some problems regarding the entities repository and i haven't figured it out what i'm missing.

这是我的应用程序组件:

Here is my Application Component:

@Singleton
@Component(
        modules = {
                AndroidModule.class,
                RepositoryModule.class
        }
)

public interface ApplicationComponent {
    void inject(AndroidApplication app);
    IDependenceyRepository dependencyRepository();
}

我的模块:

@Module
public class RepositoryModule {
    @Provides @Singleton IDependenceyRepository provideDependendencyRepository(Context context) {
        return new DependencyRepository(context);
    }
}

@Module
public class AndroidModule {
    private final AndroidApplication app;

    public AndroidModule(AndroidApplication app) {
        this.app = app;
    }

    @Provides @Singleton Context provideApplicationContext() {
        return app;
    }
}

我的AndroidApplication:

My AndroidApplication:

public class AndroidApplication extends Application {
    private ApplicationComponent component;

    @Override
    public void onCreate() {
        super.onCreate();
        setupGraph();
    }

    private void setupGraph() {
        component = DaggerApplicationComponent.builder()
                .androidModule(new AndroidModule(this))
                .repositoryModule(new RepositoryModule())
                .build();
        component.inject(this);
    }

    public ApplicationComponent component() {
        return component;
    }
}

这是我的活动:

public class MainActivity extends Activity {
    @Inject
    IDependenceyRepository repository;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.testRepository();
    }

    private void testRepository() {
        Dependency dep = new Dependency();
        dep.setDescription("XXX");
        dep.setName("AAAA");

        try {
            repository.save(dep);
            Log.d("", repository.queryAll().get(0).getName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

发生的事情是我得到了空指针异常进入存储库。

What happens is that i'm getting a null pointer exception into the repository. He is not being injected.

如果我添加此行,则:

repository = ((AndroidApplication) getApplication()).component().dependencyRepository();

它可以工作,但直接投资的意义在于不必担心这个,否则

It works, but the point of the DI it's not to have to worry about this, or am im wrong about that?

我尝试了其他示例和教程,但未能解决我的问题。

I've tried some other example and tutorials but haven't managed to solve my problem.

预先感谢

推荐答案

david.mihola的评论是正确的:为了能够初始化@Inject字段,则需要在组件中有一个方法(通常为 void inject(MyClass))。

david.mihola's comment is correct: in order to be able to have @Inject fields initialized, you need to have a method (typically void inject(MyClass)) in your component.

值得注意(不是专门针对您的问题,但可能会出现),如果您有基类:

It's worth noting (not specifically for your question, but it could come up) that if you have a base class:

public class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((AndroidApplication) getApplication()).component().inject(this);
    }
} 

在您的组件中:

void inject(BaseActivity);

注入具有自己的@Inject字段的子类不会工作:

Injecting into a subclass that has it's own @Inject fields won't work:

public class ActualActivity extends BaseActivity {
    @Inject IDependenceyRepository repo;
}

这是因为Dagger需要知道将在编译时间,而不是运行时(例如Dagger 1可以做到)。

This is because Dagger needs to know the concrete classes that will be injected at compile time, not runtime (like Dagger 1 could do).

这篇关于尚未注入Android Dagger 2依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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