Dagger 2 @Inject 返回 Null
我正在使用 java 创建多模块 android 项目。当我注入一个类时,我得到了Null
。 以下是我的代码。
SignupComponent.class
@FeatureScope
@Subcomponent(modules = SignupModule.class)
public interface SignupComponent {
@Subcomponent.Factory
interface Factory{
SignupComponent create();
}
void inject(Activity_Signup activity_signup);
SignupModule.class
@Module
public class SignupModule {
@FeatureScope
@Provides
public SignupContract.Presenter providePresenter(){
return new SignupPresenter();
}
}
Activity_Signup.class
SignupComponent signupComponent = ((SignupComponentProvider) getApplicationContext())
.provideSignupComponent();
//Inject the view
signupComponent.inject(this);
SignupPresenter.class
public class SignupPresenter implements SignupContract.Presenter {
// this injection return Null
@Inject
public SignupUseCase signupUseCase;
}
SignupUseCase.class
public class SignupUseCase {
@Inject
public SignupUseCase(){}
...
}
为什么我会得到 NPE
?
编辑
SignupModule.class
@Module
public class SignupModule {
@FeatureScope
@Provides
public SignupContract.Presenter providePresenter(){
return new SignupPresenter();
}
@FeatureScope
@Provides
public SignupUseCase provideUseCase(){
return new SignupUseCase();
}
}
I am creating multi module android project using java. I got Null
when I inject a class.
Followings are my codes.
SignupComponent.class
@FeatureScope
@Subcomponent(modules = SignupModule.class)
public interface SignupComponent {
@Subcomponent.Factory
interface Factory{
SignupComponent create();
}
void inject(Activity_Signup activity_signup);
SignupModule.class
@Module
public class SignupModule {
@FeatureScope
@Provides
public SignupContract.Presenter providePresenter(){
return new SignupPresenter();
}
}
Activity_Signup.class
SignupComponent signupComponent = ((SignupComponentProvider) getApplicationContext())
.provideSignupComponent();
//Inject the view
signupComponent.inject(this);
SignupPresenter.class
public class SignupPresenter implements SignupContract.Presenter {
// this injection return Null
@Inject
public SignupUseCase signupUseCase;
}
SignupUseCase.class
public class SignupUseCase {
@Inject
public SignupUseCase(){}
...
}
Why am I getting NPE
?
EDIT
SignupModule.class
@Module
public class SignupModule {
@FeatureScope
@Provides
public SignupContract.Presenter providePresenter(){
return new SignupPresenter();
}
@FeatureScope
@Provides
public SignupUseCase provideUseCase(){
return new SignupUseCase();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用
new SignupPresenter()
时,Dagger 并未实例化该对象,而是您实例化了该对象。因此,Dagger 不会注入@Inject
注释的字段,因为它假设您想要的对象与返回的对象完全相同。如果您希望 Dagger 填充 SignupUseCase,则需要执行以下操作之一:
让 Dagger 创建 SignupPresenter 并在您的
@Provides
方法中接收它。您需要向 SignupPresenter 添加一个带@Inject
注释的构造函数,可能不带参数。让 Dagger 创建 SignupPresenter 并使用
@Binds
方法将其绑定到 SignupContract.Presenter。这需要 SignupPresenter 上的@Inject
注释构造函数,并且还需要使您的模块成为一个抽象类。使用 MembersInjector,您可以通过注入获得它。您可能不想要这个,除非您收到一个 SignupPresenter 实例,您可以向其中添加
@Inject
方法和字段,但 Dagger 无法创建该实例。When you call
new SignupPresenter()
, Dagger isn't instantiating the object, you are. Therefore, Dagger doesn't inject the@Inject
-annotated fields, because it assumes you want the object exactly as you returned it.If you want Dagger to populate SignupUseCase, you'll need to do one of the following:
Let Dagger create SignupPresenter and receive it in your
@Provides
method. You'll need to add an@Inject
-annotated constructor to SignupPresenter, potentially with no args.Let Dagger create SignupPresenter and bind it to SignupContract.Presenter using a
@Binds
method. This requires the@Inject
-annotated constructor on SignupPresenter and also requires making your Module an abstract class.Use a MembersInjector, which you can get via injection. You probably don't want this one unless you're receiving an instance of SignupPresenter that you can add
@Inject
methods and fields to, but that Dagger can't create.