如何在Android中使用带有基本活动和片段的Dagger 2?

时间:2018-02-07 02:27:11

标签: android dagger-2

我是使用Dagger的新手,我想在我的应用中使用它。我的应用程序的层次结构非常简单,我有3个活动(A1,A2,A3),所有这些活动都从一个(BaseActivity)扩展。活动A3使用3个片段(F1,F2,F3)。这些是我定义的组件和模块:

AppComponent

@Singleton
@Component(modules = {
        AndroidInjectionModule.class,
        ActivityModule.class
})
public interface AppComponent {
    void inject(MyApp myApp);

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }
}

ActivityModude

@Module
public abstract class ActivityModule {

    @ContributesAndroidInjector
    abstract A1 contributeA1();

    @ContributesAndroidInjector
    abstract A2 contributeA2();

    @ContributesAndroidInjector(modules = FragmentBuildersModule.class)
    abstract A3 contributeA3();
}

FragmentBuildersModule

@Module
public abstract class FragmentBuildersModule {
    @ContributesAndroidInjector
    abstract F1 contributeF1();

    @ContributesAndroidInjector
    abstract F2 contributeF2();

    @ContributesAndroidInjector
    abstract F3 contributeF3();
}

当我尝试构建应用程序时,我收到此错误日志:

Error:(35, 8) error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> cannot be provided without an @Provides-annotated method.
java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> is injected at
dagger.android.DispatchingAndroidInjector.<init>(injectorFactories)
dagger.android.DispatchingAndroidInjector<android.support.v4.app.Fragment> is injected at
dagger.android.support.DaggerAppCompatActivity.supportFragmentInjector
com.we.go.cuba.ui.A1 is injected at
dagger.android.AndroidInjector.inject(arg0)
A binding with matching key exists in component: com.we.go.cuba.di.ActivityModule_ContributeA3.A3Subcomponent

我做错了什么?提前谢谢......

0 个答案:

没有答案