我有一个当前的应用程序,我正在尝试通过练习学习MVP和Dagger2,但我必须遗漏一些东西。
我有以下课程:
[MVP]
LoginActivity(查看)
public class LoginActivity extends AppCompatActivity implements LoginContract.View {
@Inject
LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
DaggerLoginComponent.builder()
.loginModule(new LoginModule(this))
.build().inject(this);
}
}
LoginPresenter(演示者)
public class LoginPresenter implements LoginContract.Presenter {
private LoginContract.View view;
@Inject LoginModel loginModel;
@Inject
public LoginPresenter(LoginContract.View view) {
this.view = view;
}
}
LoginModel(型号)
public class LoginModel {
Context context;
@Inject
public LoginModel(Context context){
this.context=context;
}
}
[Dagger 2]
ActivityScope
@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}
的ApplicationContext
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationContext {
}
ApplicationComponent
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
@ApplicationContext
Context getContext();
@ApplicationContext
BloggerPro getBloggerProApp();
}
ApplicationModule
@Module
public class ApplicationModule {
private final Application mApplication;
public ApplicationModule(Application mApplication) {
this.mApplication = mApplication;
}
@Provides
@ApplicationContext
Context provideContext() {
return mApplication;
}
}
LoginComponent
@ActivityScope
@Component(modules = {ApplicationModule.class, LoginModule.class})
public interface LoginComponent extends ApplicationComponent {
void inject(LoginActivity loginActivity);
}
LoginModule
@Module
public class LoginModule {
private final LoginContract.View mView;
public LoginModule(LoginContract.View view){
this.mView=view;
}
@Provides
@ActivityScope
LoginContract.View providesMainScreenContractView() {
return mView;
}
}
当我编译应用程序时,我收到以下错误:
Error:(19, 10) error: android.content.Context cannot be provided without an @Provides-annotated method.
android.content.Context is injected at
com.bloggerpro.android.app.features.login.LoginModel.<init>(context)
com.bloggerpro.android.app.features.login.LoginModel is injected at
com.bloggerpro.android.app.features.login.LoginPresenter.loginModel
com.bloggerpro.android.app.features.login.LoginPresenter is injected at
com.bloggerpro.android.app.features.login.LoginActivity.presenter
com.bloggerpro.android.app.features.login.LoginActivity is injected at
com.bloggerpro.android.di.components.LoginComponent.inject(loginActivity)
我想要做的是拥有单独的组件并将它们组合起来,但到目前为止我还没有成功。
已经尝试使LoginComponent从ApplicationComponent继承,但也没有骰子。