简体   繁体   English

Dagger2 - null而不是注入对象

[英]Dagger2 - null instead of injected object

For making things simple, suppose I want to inject EmailValidator from apache validators into my activity: 为简单起见,假设我想将apache验证器中的EmailValidator注入我的活动中:

public class MainActivity extends FragmentActivity {

    @Inject
    EmailValidator emailValidator;

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

I have a MainModule class: 我有一个MainModule类:

@Module
public class MainModule {

    @Provides
    public EmailValidator providesEmailValidator() {
        return EmailValidator.getInstance();
    }
}

and MainComponent interface: 和MainComponent接口:

@Singleton
@Component(modules = MainModule.class)
public interface MainComponent {

    EmailValidator getEmailValidator();
}

When trying to use my validator in activity, I'm getting a nullpointer exception: 当我尝试在活动中使用我的验证器时,我得到一个nullpointer异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.apache.commons.validator.routines.EmailValidator.isValid(java.lang.String)' on a null object reference

Obviously I'm missing something. 显然我错过了一些东西。 I know that dagger creates component implementation for me. 我知道匕首为我创建了组件实现。 Should I use it? 我应该用吗? How? 怎么样?

If I do the following in my onCreate method: 如果我在onCreate方法中执行以下操作:

        emailValidator = Dagger_MainComponent.create().getEmailValidator();

then everything works fine. 一切正常。

But I want to be able to use @Inject annotation anywhere (probably on setter/constructor instead of a field) instead. 但我希望能够在任何地方使用@Inject注释(可能在setter / constructor而不是字段上)。

What am I missing? 我错过了什么?

I did something similar with dagger1 and it worked. 我用dagger1做了类似的事情并且它起作用了。 Of course I needed to call ObjecGraph.inject(this) in activity. 当然我需要在活动中调用ObjecGraph.inject(this) What's the dagger2 equivalent? 什么是dagger2等价物?

EDIT: 编辑:

Ok, so I've found a solution. 好的,所以我找到了解决方案。 If anyone will ever have such a problem, there are some snippets: 如果有人会有这样的问题,有一些片段:

1) I've created an application class: 1)我创建了一个应用程序类:

public class EmailSenderApplication extends Application {

    private MainComponent component;

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

        component = Dagger_MainComponent
                .create();

        component.inject(this);
    }

    public MainComponent component() {
        return component;
    }
}

2) In AndroidManifest.xml: 2)在AndroidManifest.xml中:

<application
        android:name=".EmailSenderApplication"
        ...

3) And finally, in the activity class where I want to inject some components those two ugly as hell lines: 3)最后,在活动类中,我想要注入一些组件,这两个组件是丑陋的地狱线:

component = ((EmailSenderApplication) getApplication()).component();
component.inject(this);

Looks like you need to build your component as in: 看起来您需要构建组件,如下所示:

component = Dagger_ MainComponent.builder()
        .mainModule(new MainModule())
        .build();

Typically, you do this in the onCreate method of your Application. 通常,您可以在应用程序的onCreate方法中执行此操作。

One good resource that may help you is the example apps in the Dagger 2 repo . 可以帮助您的一个好资源是Dagger 2 repo中示例应用程序

I also found this PR helpful, from a suggested update to Jake Wharton's u2020 sample app (from the main Dagger 2 Engineer). 我还发现这个PR很有帮助,从建议的更新到Jake Wharton的u2020示例应用程序 (来自主Dagger 2工程师)。 It gives a good overview of the changes you need to make when going from Dagger 1 to 2 and, apparently that's what he points people to as well . 它很好地概述了从Dagger 1到2时需要做出的改变,显然这也是他对人们的看法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM