我不知道为什么mainRankPresenter中由dagger2注入的MainRankPresenter.java的loginResponseHandler为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());
}
}
答案 0 :(得分:1)
Dagger 2的注入不是递归的。因此,当您在inject(this)
中调用RankFragment
时,只会注入该片段的@Inject
个注释字段。 Dagger 2不会在注入的对象中搜索@Inject
注释。
通常,您应该尝试将依赖注入框架的使用限制为"顶级"组件(活动,片段,服务等),由Android框架为您实例化。在您自己实例化的对象中(如MainRankPresenter
),您应该使用其他不涉及外部框架的DI技术(例如,依赖注入构造函数)。
答案 1 :(得分:0)
因为你@Provides
MainRankPresenter
,Dagger不会注射它:你要对此承担责任。您可以将您的提供方法传递给MembersInjector
,如果对象在返回之前可以注入字段,但它可能更好地重构您移除的模块,提供方法并让Dagger处理注射(你已经拥有了所有@Inject
)