Dagger2 - null 而不是注入的对象
Dagger2 - null instead of injected object
为了简单起见,假设我想将 Apache 验证器中的 EmailValidator 注入我的 activity:
public class MainActivity extends FragmentActivity {
@Inject
EmailValidator emailValidator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
我有一个 MainModule class:
@Module
public class MainModule {
@Provides
public EmailValidator providesEmailValidator() {
return EmailValidator.getInstance();
}
}
和 MainComponent 接口:
@Singleton
@Component(modules = MainModule.class)
public interface MainComponent {
EmailValidator getEmailValidator();
}
当尝试在 activity 中使用我的验证器时,我遇到了空指针异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.apache.commons.validator.routines.EmailValidator.isValid(java.lang.String)' on a null object reference
显然我遗漏了一些东西。我知道 dagger 为我创建组件实现。我应该使用它吗?怎么样?
如果我在 onCreate 方法中执行以下操作:
emailValidator = Dagger_MainComponent.create().getEmailValidator();
然后一切正常。
但我希望能够在任何地方(可能在 setter/constructor 而不是字段上)使用 @Inject 注释。
我错过了什么?
我对 dagger1 做了类似的事情并且它起作用了。当然,我需要在 activity 中调用 ObjecGraph.inject(this)
。什么是 dagger2 等价物?
编辑:
好的,所以我找到了解决方案。如果有人遇到过这样的问题,这里有一些片段:
1) 我创建了一个应用程序 class:
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) 在AndroidManifest.xml:
<application
android:name=".EmailSenderApplication"
...
3) 最后,在 activity class 中,我想注入一些组件,那两条丑陋得要命的行:
component = ((EmailSenderApplication) getApplication()).component();
component.inject(this);
看起来您需要构建组件,如下所示:
component = Dagger_ MainComponent.builder()
.mainModule(new MainModule())
.build();
通常,您在应用程序的 onCreate
方法中执行此操作。
example apps in the Dagger 2 repo 可能对您有帮助的一个很好的资源。
我还发现此 PR 很有帮助,来自 suggested update to Jake Wharton's u2020 sample app (from the main Dagger 2 Engineer). 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。
为了简单起见,假设我想将 Apache 验证器中的 EmailValidator 注入我的 activity:
public class MainActivity extends FragmentActivity {
@Inject
EmailValidator emailValidator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
我有一个 MainModule class:
@Module
public class MainModule {
@Provides
public EmailValidator providesEmailValidator() {
return EmailValidator.getInstance();
}
}
和 MainComponent 接口:
@Singleton
@Component(modules = MainModule.class)
public interface MainComponent {
EmailValidator getEmailValidator();
}
当尝试在 activity 中使用我的验证器时,我遇到了空指针异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.apache.commons.validator.routines.EmailValidator.isValid(java.lang.String)' on a null object reference
显然我遗漏了一些东西。我知道 dagger 为我创建组件实现。我应该使用它吗?怎么样?
如果我在 onCreate 方法中执行以下操作:
emailValidator = Dagger_MainComponent.create().getEmailValidator();
然后一切正常。
但我希望能够在任何地方(可能在 setter/constructor 而不是字段上)使用 @Inject 注释。
我错过了什么?
我对 dagger1 做了类似的事情并且它起作用了。当然,我需要在 activity 中调用 ObjecGraph.inject(this)
。什么是 dagger2 等价物?
编辑:
好的,所以我找到了解决方案。如果有人遇到过这样的问题,这里有一些片段:
1) 我创建了一个应用程序 class:
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) 在AndroidManifest.xml:
<application
android:name=".EmailSenderApplication"
...
3) 最后,在 activity class 中,我想注入一些组件,那两条丑陋得要命的行:
component = ((EmailSenderApplication) getApplication()).component();
component.inject(this);
看起来您需要构建组件,如下所示:
component = Dagger_ MainComponent.builder()
.mainModule(new MainModule())
.build();
通常,您在应用程序的 onCreate
方法中执行此操作。
example apps in the Dagger 2 repo 可能对您有帮助的一个很好的资源。
我还发现此 PR 很有帮助,来自 suggested update to Jake Wharton's u2020 sample app (from the main Dagger 2 Engineer). 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。