在MVP Presenter Android中注入上下文

时间:2017-12-04 14:09:26

标签: java android mvp dagger-2

我正在使用MVP架构和Dagger2。我想使用构造函数注入在我的Presenter中获取Context。

我的活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appintroduction);
        activityComponent().inject(this);
        ButterKnife.bind(this);
        skipall.bringToFront();
     }

我的演示者:

@ConfigPersistent

    public class AppIntroPresenter extends BasePresenter<AppIntroMvpView> {

        private final DataManager mDataManager;
        private final Context ctx1;


        @Inject
        public AppIntroPresenter(DataManager dataManager,Context context) {
            mDataManager = dataManager;
            ctx1=context;

        }

        public void openCamView(Context ctx) {

            ctx.startActivity(new Intent(ctx, CameraActivity.class));
        }
    }

我的应用程序组件:

@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {

    void inject(SyncService syncService);

    @ApplicationContext Context context();
    Application application();
    RibotsService ribotsService();
    PreferencesHelper preferencesHelper();
    DatabaseHelper databaseHelper();
    DataManager dataManager();
    RxEventBus eventBus();
    ClassService classService();
    AnsweredQuestionService answeredQuestionService();
}

我的活动组件:

@PerActivity
@Subcomponent(modules = ActivityModule.class)
public interface ActivityComponent {

    void inject(MainActivity mainActivity);
    void inject(CameraActivity cameraActivity);
    void inject(AppIntroActivity appIntroActivity);

}

我的申请模块:

@Module
public class ApplicationModule {
    protected final Application mApplication;

    public ApplicationModule(Application application) {
        mApplication = application;
    }

    @Provides
    Application provideApplication() {
        return mApplication;
    }

    @Provides
    @ApplicationContext
    Context provideContext() {
        return mApplication;
    }

    @Provides
    @Singleton
    RibotsService provideRibotsService() {
        return RibotsService.Creator.newRibotsService();
    }

    @Provides
    @Singleton
    ClassService provideClassService() {
        return ClassService.Creator.newClassService();
    }

    @Provides
    @Singleton
    AnsweredQuestionService provideAnsweredQuestionService() {
        return AnsweredQuestionService.Creator.newAnsweredQuestionService();
    }



}

我的活动模块:

@Module
public class ActivityModule {

    private Activity mActivity;

    public ActivityModule(Activity activity) {
        mActivity = activity;
    }

    @Provides
    Activity provideActivity() {
        return mActivity;
    }

}

运行代码后,我收到错误:

Error:(17, 8) error: [uk.co.ribot.androidboilerplate.injection.component.ActivityComponent.inject(uk.co.ribot.androidboilerplate.ui.AppIntro.AppIntroActivity)] android.content.Context cannot be provided without an @Provides-annotated method.
android.content.Context is injected at
uk.co.ribot.androidboilerplate.ui.AppIntro.AppIntroPresenter.<init>(…, context)
uk.co.ribot.androidboilerplate.ui.AppIntro.AppIntroPresenter is injected at
uk.co.ribot.androidboilerplate.ui.AppIntro.AppIntroActivity.mAppIntroPresenter
uk.co.ribot.androidboilerplate.ui.AppIntro.AppIntroActivity is injected at
uk.co.ribot.androidboilerplate.injection.component.ActivityComponent.inject(appIntroActivity)

我知道当我在My Presenter中为构造函数注入添加上下文时我得到了这个错误,但我无法弄清楚为什么?如果我从构造函数中删除上下文:

@Inject
        public AppIntroPresenter(DataManager dataManager,Context context) {
            mDataManager = dataManager;
            ctx1=context;

        }

然后一切正常。但我需要注射的背景。请帮帮我吗?

1 个答案:

答案 0 :(得分:0)

由于您的构造函数具有成员,因此您需要创建一个模块并在其中创建一个使用@Provides@Singleton注释的实例,与ApplicationModule相同。