不知道为什么dagger2注入的对象在presenter中为null
I don't know why the object injected by dagger2 is null in presenter
不知道为什么dagger2注入的MainRankPresenter.java的loginResponseHandler在MainRankPresenter中为null。
我只想注入字段进行字段注入。
我应该做其他方式而不是现场注入吗?
请让我知道如何解决它。
BBBApplication.java
public class BBBApplication extends MultiDexApplication
{
...
@Override
public void onCreate() {
super.onCreate();
initAppComponent();
}
private void initAppComponent() {
this.appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
public static BBBApplication get(Context ctx) {
return (BBBApplication) ctx.getApplicationContext();
}
public AppComponent getAppComponent() {
return this.appComponent;
}
...
}
AppModule.java
@Module
public class AppModule {
private BBBApplication application;
public AppModule(BBBApplication application) {
this.application = application;
}
@Provides
@Singleton
public Application provideApplication() {
return this.application;
}
@Provides
@Singleton
public Resources provideResources() {
return this.application.getResources();
}
@Provides`enter code here`
@Singleton
public SharedPreferences provideSharedPreferences() {
return PreferenceManager.getDefaultSharedPreferences(this.application);
}
}
AppComponent.java
@Singleton
@Component(modules = {AppModule.class, ServiceModule.class})
public interface AppComponent {
RankFragmentComponent plus(RankFragmentModule module);
Application application();
Resources resources();
}
RankFragmentModule.java
@Module
public class RankFragmentModule {
private RankFragment rankFragment;
public RankFragmentModule(RankFragment rankFragment) {
this.rankFragment = rankFragment;
}
@Provides
@ActivityScope
public LoginResponseHandler provideLoginResponseHandler() {
return new LoginResponseHandler(this.rankFragment);
}
@Provides
@ActivityScope
// @Named("rankFragment")
public RankFragment provideRankFragment() {
return this.rankFragment;
}
@Provides
@ActivityScope
public MainRankPresenter provideMainRankPresenter(RankFragment rankFragment) {
return new MainRankPresenter(new MainRankViewOps(rankFragment));
}
}
RankFragmentComponent.java
@ActivityScope
@Subcomponent(modules = {RankFragmentModule.class})
public interface RankFragmentComponent {
void inject(RankFragment rankFragment);
}
RankFragment.java
public class RankFragment extends Fragment {
@Inject
MainRankPresenter presenter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BBBApplication.get(getContext())
.getAppComponent()
.plus(new RankFragmentModule(this))
.inject(this);
presenter.test();
}
MainRankPresenter.java
public class MainRankPresenter implements Presenter {
private MainRankViewOps viewOps;
@Inject
LoginResponseHandler loginResponseHandler;
@Inject
public MainRankPresenter(MainRankViewOps viewOps) {
this.viewOps = viewOps;
}
@Override
public void test() {
Log.d(Constants.TAG_DEBUG, "=== presenter test" + this.toString());
Log.d(Constants.TAG_DEBUG, "=== " + loginResponseHandler.toString());
this.viewOps.initViewOps();
}
}
MainRankViewOps.java
public class MainRankViewOps implements ViewOps {
RankFragment fragment;
@Inject
public MainRankViewOps(RankFragment fragment) {
this.fragment = fragment;
}
@Override
public void initViewOps() {
Log.d(Constants.TAG_DEBUG, "=== view ops" + this.toString());
Log.d(Constants.TAG_DEBUG, "=== " + fragment.toString());
}
}
Dagger 2 的注入不是递归的。因此,当您在 RankFragment
中调用 inject(this)
时,只会注入该片段的 @Inject
注释字段。 Dagger 2 不会在注入的对象中搜索 @Inject
注释。
一般来说,您应该尝试将依赖注入框架的使用限制在 "top-level" 组件(活动、片段、服务等),这些组件由 Android 框架为您实例化。在您自己实例化的对象中(如 MainRankPresenter
),您应该使用其他不涉及外部框架的 DI 技术(例如,向构造函数中注入依赖项)。
因为您 @Provides
MainRankPresenter
,Dagger 不会注入它:您对此负责。你可能让你的 provides 方法被传递一个 MembersInjector
si 你可以在返回它之前注入字段,但是最好重构你删除的提供方法的模块并让 Dagger 处理注入(您已经拥有所有需要的 @Inject
)
不知道为什么dagger2注入的MainRankPresenter.java的loginResponseHandler在MainRankPresenter中为null。 我只想注入字段进行字段注入。 我应该做其他方式而不是现场注入吗? 请让我知道如何解决它。
BBBApplication.java
public class BBBApplication extends MultiDexApplication
{
...
@Override
public void onCreate() {
super.onCreate();
initAppComponent();
}
private void initAppComponent() {
this.appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
public static BBBApplication get(Context ctx) {
return (BBBApplication) ctx.getApplicationContext();
}
public AppComponent getAppComponent() {
return this.appComponent;
}
...
}
AppModule.java
@Module
public class AppModule {
private BBBApplication application;
public AppModule(BBBApplication application) {
this.application = application;
}
@Provides
@Singleton
public Application provideApplication() {
return this.application;
}
@Provides
@Singleton
public Resources provideResources() {
return this.application.getResources();
}
@Provides`enter code here`
@Singleton
public SharedPreferences provideSharedPreferences() {
return PreferenceManager.getDefaultSharedPreferences(this.application);
}
}
AppComponent.java
@Singleton
@Component(modules = {AppModule.class, ServiceModule.class})
public interface AppComponent {
RankFragmentComponent plus(RankFragmentModule module);
Application application();
Resources resources();
}
RankFragmentModule.java
@Module
public class RankFragmentModule {
private RankFragment rankFragment;
public RankFragmentModule(RankFragment rankFragment) {
this.rankFragment = rankFragment;
}
@Provides
@ActivityScope
public LoginResponseHandler provideLoginResponseHandler() {
return new LoginResponseHandler(this.rankFragment);
}
@Provides
@ActivityScope
// @Named("rankFragment")
public RankFragment provideRankFragment() {
return this.rankFragment;
}
@Provides
@ActivityScope
public MainRankPresenter provideMainRankPresenter(RankFragment rankFragment) {
return new MainRankPresenter(new MainRankViewOps(rankFragment));
}
}
RankFragmentComponent.java
@ActivityScope
@Subcomponent(modules = {RankFragmentModule.class})
public interface RankFragmentComponent {
void inject(RankFragment rankFragment);
}
RankFragment.java
public class RankFragment extends Fragment {
@Inject
MainRankPresenter presenter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BBBApplication.get(getContext())
.getAppComponent()
.plus(new RankFragmentModule(this))
.inject(this);
presenter.test();
}
MainRankPresenter.java
public class MainRankPresenter implements Presenter {
private MainRankViewOps viewOps;
@Inject
LoginResponseHandler loginResponseHandler;
@Inject
public MainRankPresenter(MainRankViewOps viewOps) {
this.viewOps = viewOps;
}
@Override
public void test() {
Log.d(Constants.TAG_DEBUG, "=== presenter test" + this.toString());
Log.d(Constants.TAG_DEBUG, "=== " + loginResponseHandler.toString());
this.viewOps.initViewOps();
}
}
MainRankViewOps.java
public class MainRankViewOps implements ViewOps {
RankFragment fragment;
@Inject
public MainRankViewOps(RankFragment fragment) {
this.fragment = fragment;
}
@Override
public void initViewOps() {
Log.d(Constants.TAG_DEBUG, "=== view ops" + this.toString());
Log.d(Constants.TAG_DEBUG, "=== " + fragment.toString());
}
}
Dagger 2 的注入不是递归的。因此,当您在 RankFragment
中调用 inject(this)
时,只会注入该片段的 @Inject
注释字段。 Dagger 2 不会在注入的对象中搜索 @Inject
注释。
一般来说,您应该尝试将依赖注入框架的使用限制在 "top-level" 组件(活动、片段、服务等),这些组件由 Android 框架为您实例化。在您自己实例化的对象中(如 MainRankPresenter
),您应该使用其他不涉及外部框架的 DI 技术(例如,向构造函数中注入依赖项)。
因为您 @Provides
MainRankPresenter
,Dagger 不会注入它:您对此负责。你可能让你的 provides 方法被传递一个 MembersInjector
si 你可以在返回它之前注入字段,但是最好重构你删除的提供方法的模块并让 Dagger 处理注入(您已经拥有所有需要的 @Inject
)