//模块
@Module
public class PresenterModule {
@Provides
@PerActivity
public MainPresenter provideMainPresenter() {
return new MainPresenter();
}
@Provides
@PerActivity
public PreLoginPresenter providePreLoginPresenter() {
return new PreLoginPresenter();
}
@Provides
@PerActivity
public NotificationPresenter provideNotificationPresenter() {
return new NotificationPresenter();
}
}
@Module
public class UserLoginModule {
@Provides
@PerActivity
public UserModel getUser(){
return new UserModel();
}
}
//组件
@PerActivity
@Component(modules = {UserLoginModule.class})
public interface UserLoginComponent {
void injectSharedPreferences(SharedPreferences sharedPreferences);
void injectPreLoginPresenter(PreLoginActivity preLoginActivity);
}
@PerActivity
@Component(modules = {PresenterModule.class})
public interface PresenterComponent {
//void injectMainPresenter(MainActivity mainActivity);
void injectPreLoginPresenter(PreLoginActivity preLoginActivity);
//void injectNotificationPresenter(NotificationActivity notificationActivity);
}
`
// PRELOGINPRESENTER
public class PreLoginPresenter {
@Inject
UserModel userModel;
public String onStateSelected(String state) {
userModel.setState(state);
return userModel.getState();
}
}
// PRELOGINACTIVITY`
//在里面
//Instantiate dagger 2
PresenterComponent presenterComponent = DaggerPresenterComponent.builder()
.build();
presenterComponent.injectPreLoginPresenter(PreLoginActivity.this);//passar o contexto para o componente
//Instantiate dagger 2
UserLoginComponent userLoginComponent = DaggerUserLoginComponent.builder()
.build();
userLoginComponent.injectPreLoginPresenter(PreLoginActivity.this);//passar o contexto para o componente``
//错误日志
错误:(18、53)错误:找不到符号类
DaggerPresenterComponent错误:(19,53)错误:找不到符号
DaggerUserLoginComponent类错误:(19,10)错误:
gorick.gradesprojectandroid.MVP.Presenter.Presenters.PreLoginPresenter
没有@Inject构造函数或无法通过
@ Provides-或@ Produces-带注释的方法。此类型支持成员
注入,但不能隐式提供。
gorick.gradesprojectandroid.MVP.Presenter.Presenters.PreLoginPresenter
在注入
gorick.gradesprojectandroid.MVP.View.PreLoginActivity.preLoginPresenter
gorick.gradesprojectandroid.MVP.View.PreLoginActivity在以下位置注入
gorick.gradesprojectandroid.Dagger2.Component.UserLoginComponent.injectPreLoginPresenter(preLoginActivity)
最佳答案
您已经在PresenterModule中绑定了PreLoginPresenter,但是尚未在UserLoginComponent中安装它。这意味着您的UserLoginComponent没有将PreLoginPresenter注入PreLoginActivity所需的绑定,因此代码生成失败,并向您显示该错误消息。
您永远不需要使用两个不同的组件来注入相同的类。无法表示一个组件满足某些绑定,而另一组件满足其他绑定。相反,请确保您有一个涵盖所有内容的组件,然后使用该组件进行注入。