Dagger 2从活动模块中依赖注入片段 - 存在匹配键

时间:2018-02-08 06:27:33

标签: android dependency-injection dagger-2 dagger

使用Dagger 2.11 Dagger *类我似乎无法为我的Fragments提供在父活动模块中声明的依赖项。

我收到以下错误:

Error:(21, 8) error: [dagger.android.AndroidInjector.inject(T)] 
@javax.inject.Named("ENTITY_KEY") java.lang.String cannot be provided 
without an @Provides- or @Produces-annotated method.
@javax.inject.Named("ENTITY_KEY") java.lang.String is injected at
com.cragchat.mobile.ui.view.fragments.CommentSectionFragment.mEntityKey
com.cragchat.mobile.ui.view.fragments.CommentSectionFragment is injected at
com.cragchat.mobile.ui.view.activity.RouteActivity.commentSectionFragment
com.cragchat.mobile.ui.view.activity.RouteActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
A binding with matching key exists in component: 
com.cragchat.mobile.di.ActivityBinding_AreaActivity.AreaActivitySubcomponent

正如我理解这个错误一样,Dagger认识到存在正确密钥的依赖关系,但它在AreaActivitySubcomponent中由于某种原因而无法通过CommentSecctionFragment子组件访问。

ApplicationComponent.java:

@Singleton
@Component(modules = {
        AndroidSupportInjectionModule.class,
        ApplicationModule.class,
        ActivityBinding.class})
public interface ApplicationComponent extends 
AndroidInjector<CragChatApplication> {

@Component.Builder
interface Builder {

    @BindsInstance
    ApplicationComponent.Builder application(Application application);

    ApplicationComponent build();
}

void inject(CragChatApplication app);
}

ActivityBinding.java:

@Module
public abstract class ActivityBinding {

    @ActivityScoped
    @ContributesAndroidInjector(modules = MainActivityModule.class)
    abstract MainActivity mainActivity();

    @ActivityScoped
    @ContributesAndroidInjector(modules = {AreaModule.class, 
         AreaFragmentsModule.class})
    abstract AreaActivity areaActivity();
}

AreaModule.java

@Module
public class AreaModule {

@Provides
Area provideArea(AreaActivity activity) {
    return activity.getIntent().getParcelableExtra(NavigationUtil.ENTITY);
}

@Provides
@Named(InjectionNames.ENTITY_KEY)
String entityKey(Area area) {
    return area.getKey();
}

@Provides
@Named(InjectionNames.AREA_IDS)
String[] areaIds(Area area) {
    return area.getSubAreas().toArray(new String[area.getSubAreas().size()]);
}

@Provides
@Named(InjectionNames.ROUTE_IDS)
String[] routeIds(Area area) {
    return area.getRoutes().toArray(new String[area.getRoutes().size()]);
}

}

AreaFragmentsModule.java

@Module
public abstract class AreaFragmentsModule {

@FragmentScoped
@ContributesAndroidInjector
abstract RecentActivityFragment recentActivityFragment();

@FragmentScoped
@ContributesAndroidInjector
abstract AreaListFragment areaListFragment();

@FragmentScoped
@ContributesAndroidInjector
abstract CommentSectionFragment commentSectionFragment();

@FragmentScoped
@ContributesAndroidInjector
abstract ImageFragment imageFragment();

}

CommentSectionFragment.java:

public class CommentSectionFragment extends DaggerFragment implements 
View.OnClickListener {

/* -------------  This injection causes the error!!!!! ------------ */
@Inject
@Named(InjectionNames.ENTITY_KEY)
String mEntityKey;

/*
    Repository and Authentication inject correctly from ApplicationModule
*/
@Inject
Repository mRepository;

@Inject
Authentication mAuthentication;

@Inject
public CommentSectionFragment() {
}

/* .... */
}

0 个答案:

没有答案