简体   繁体   English

Dagger 2.11 - 组件中存在具有匹配键的绑定

[英]Dagger 2.11 - A binding with matching key exists in component

I have a small scenario where I have this following structure where I'm trying to inject fragment manager in baseActivity Fragment but for some reason I'm running out of luck:(我有一个小场景,我有以下结构,我试图在 baseActivity Fragment 中注入片段管理器,但由于某种原因我运气不好:(

@Singleton
@Component(modules = { AppModule.class,
        ActivityModule.class,
        AndroidSupportInjectionModule.class })
public interface AppComponent extends AndroidInjector<App> {

    @Override
    void inject(App application);

    @Component.Builder interface Builder {

        @BindsInstance
        AppComponent.Builder application(App application);

        AppComponent build();
    }
}

ActivityModule.class ActivityModule.class

@PerActivity
@ContributesAndroidInjector(modules = BaseActivityModule.class)
abstract BaseActivity baseActivity();

BaseActivityModule.class BaseActivityModule.class

static final String ACTIVITY_FRAGMENT_MANAGER = "ACTIVITY_FRAGMENT_MANAGER";

@PerActivity
@Named(ACTIVITY_FRAGMENT_MANAGER)
@Provides
static FragmentManager activityFragmentManager(BaseActivity activity) {
    return activity.getSupportFragmentManager();
}

BaseAcitivity.class BaseAcitivity.class

public abstract class BaseActivity extends DaggerAppCompatActivity {

    @Named(ACTIVITY_FRAGMENT_MANAGER)
    @Inject
    FragmentManager fragmentManager;
}

So even though i'm providing my fragment manager in BaseActivityModule.class dagger is throwing this following error.因此,即使我在 BaseActivityModule.class 中提供我的片段管理器,匕首也会抛出以下错误。 I even tried with just Activity instead of BaseActivity as my input parameter in BaseActivityModule.我什至尝试只使用 Activity 而不是 BaseActivity 作为我在 BaseActivityModule 中的输入参数。 Even then I land up in this same issue.即便如此,我还是陷入了同样的问题。 Not sure what exactly I'm screwing up.不确定我到底搞砸了什么。 So any help is appreciated.所以任何帮助表示赞赏。 Thanks in advance:)提前致谢:)

Error:(17, 8) error: [dagger.android.AndroidInjector.inject(T)] @javax.inject.Named("ACTIVITY_FRAGMENT_MANAGER") android.support.v4.app.FragmentManager cannot be provided without an @Provides- or @Produces-annotated method.
@javax.inject.Named("ACTIVITY_FRAGMENT_MANAGER") android.support.v4.app.FragmentManager is injected at
com.abc.views.base.BaseActivity.fragmentManager
com.abc.views.def.ABCActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
A binding with matching key exists in component: om.abc.views.base.BaseActivity_BaseActivity.BaseActivitySubcomponent

"A binding with matching key exists in component" means that you have bound a dependency somewhere in your entire object graph but it cannot be reached from the subcomponent where it needs to be injected. “组件中存在与匹配键的绑定”意味着您已在整个对象图中的某处绑定了一个依赖项,但无法从需要注入它的子组件中访问它。 Here is the javadoc : 这是javadoc

Utility code that looks for bindings matching a key in all subcomponents in a binding graph so that a user is advised that a binding exists elsewhere when it is not found in the current subgraph. 实用程序代码,用于查找与绑定图中所有子组件中的键匹配的绑定,以便在当前子图中找不到绑定时,建议用户在其他位置存在绑定。 If a binding matching a key exists in a sub- or sibling component, that is often what the user actually wants to use. 如果匹配键的绑定存在于子组件或兄弟组件中,那通常是用户实际想要使用的。

For instance, assume you have two Activities, ActivityA and ActivityB . 例如,假设您有两个活动, ActivityAActivityB You generate subcomponents with @ContributesAndroidInjector and bind Foo in the ActivityA module but not the ActivityB module. 使用@ContributesAndroidInjector生成子组件并在ActivityA模块中绑定Foo ,但不绑定ActivityB模块。 If you request injection for Foo in ActivityB with @Inject Foo foo you will get that error message. 如果您使用@Inject Foo foo请求ActivityB中的Foo注入,您将收到该错误消息。

Forming subcomponents using @ContributesAndroidInjector on a base class like BaseActivity is probably not a good approach here. 在像BaseActivity这样的基类上使用@ContributesAndroidInjector形成子组件可能不是一个好方法。 Like in the comment from David Medenjak , the subcomponent for the base class will be ignored and the subcomponent for the concrete class will perform injection on ABCActivity . David Medenjak的注释一样,基类的子组件将被忽略,具体类的子组件将对ABCActivity执行注入。

For now, you can fix your error by binding FragmentManager in the subcomponent for ABCActivity : 现在,您可以通过在ABCActivity的子组件中绑定FragmentManager来修复错误:

@PerActivity
@ContributesAndroidInjector(modules = BaseActivityModule.class)
abstract ABCActivity ABCActivity();

Did you try provide without static ? 你试过没有静电吗?

@Module(includes = {ViewModelModule.class})
public class AppModule {

@Provides
public PrefManager providePrefManager(Application app) {
    return PrefManager.getInstance(app);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM