我有一个当前的应用程序,我正在尝试通过练习来学习 MVP 和 Dagger2,但我必须遗漏一些东西。
我有以下课程:
[MVP]
登录活动(查看)
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);
}
}
登录演示者(演示者)
public class LoginPresenter implements LoginContract.Presenter {
private LoginContract.View view;
@Inject LoginModel loginModel;
@Inject
public LoginPresenter(LoginContract.View view) {
this.view = view;
}
}
登录模型(模型)
public class LoginModel {
Context context;
@Inject
public LoginModel(Context context){
this.context=context;
}
}
[匕首 2]
活动范围
@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}
应用程序上下文
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationContext {
}
应用组件
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
@ApplicationContext
Context getContext();
@ApplicationContext
BloggerPro getBloggerProApp();
}
应用模块
@Module
public class ApplicationModule {
private final Application mApplication;
public ApplicationModule(Application mApplication) {
this.mApplication = mApplication;
}
@Provides
@ApplicationContext
Context provideContext() {
return mApplication;
}
}
登录组件
@ActivityScope
@Component(modules = {ApplicationModule.class, LoginModule.class})
public interface LoginComponent extends ApplicationComponent {
void inject(LoginActivity loginActivity);
}
登录模块
@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 继承,但也没有骰子。