Dagger 2.11 - 组件中存在具有匹配键的绑定
Dagger 2.11 - A binding with matching key exists in component
我有一个小场景,我有以下结构,我试图在 baseActivity 片段中注入片段管理器,但由于某种原因,我 运行 运气不好:(
@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
@PerActivity
@ContributesAndroidInjector(modules = BaseActivityModule.class)
abstract BaseActivity baseActivity();
基地ActivityModule.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
public abstract class BaseActivity extends DaggerAppCompatActivity {
@Named(ACTIVITY_FRAGMENT_MANAGER)
@Inject
FragmentManager fragmentManager;
}
所以即使我在 BaseActivityModule.class 中提供我的片段管理器,dagger 也会抛出以下错误。我什至尝试只使用 Activity 而不是 BaseActivity 作为我在 BaseActivityModule 中的输入参数。即便如此,我还是陷入了同样的问题。不确定我到底搞砸了什么。所以任何帮助表示赞赏。提前致谢:)
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" 表示您在整个对象图中的某处绑定了一个依赖项,但无法从需要注入它的子组件到达它。这是 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.
例如,假设您有两个活动,ActivityA
和 ActivityB
。您使用 @ContributesAndroidInjector
生成子组件并在 ActivityA
模块中绑定 Foo
而不是 ActivityB
模块。如果您请求使用 @Inject Foo foo
在 ActivityB
中注入 Foo
,您将收到该错误消息。
在像 BaseActivity
这样的基础 class 上使用 @ContributesAndroidInjector
形成子组件可能不是一个好的方法。就像 David Medenjak 的评论一样,基础 class 的子组件将被忽略,具体 class 的子组件将在 ABCActivity
上执行注入。
现在,您可以通过在 ABCActivity
:
的子组件中绑定 FragmentManager
来修复错误
@PerActivity
@ContributesAndroidInjector(modules = BaseActivityModule.class)
abstract ABCActivity ABCActivity();
我有一个小场景,我有以下结构,我试图在 baseActivity 片段中注入片段管理器,但由于某种原因,我 运行 运气不好:(
@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
@PerActivity
@ContributesAndroidInjector(modules = BaseActivityModule.class)
abstract BaseActivity baseActivity();
基地ActivityModule.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
public abstract class BaseActivity extends DaggerAppCompatActivity {
@Named(ACTIVITY_FRAGMENT_MANAGER)
@Inject
FragmentManager fragmentManager;
}
所以即使我在 BaseActivityModule.class 中提供我的片段管理器,dagger 也会抛出以下错误。我什至尝试只使用 Activity 而不是 BaseActivity 作为我在 BaseActivityModule 中的输入参数。即便如此,我还是陷入了同样的问题。不确定我到底搞砸了什么。所以任何帮助表示赞赏。提前致谢:)
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" 表示您在整个对象图中的某处绑定了一个依赖项,但无法从需要注入它的子组件到达它。这是 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.
例如,假设您有两个活动,ActivityA
和 ActivityB
。您使用 @ContributesAndroidInjector
生成子组件并在 ActivityA
模块中绑定 Foo
而不是 ActivityB
模块。如果您请求使用 @Inject Foo foo
在 ActivityB
中注入 Foo
,您将收到该错误消息。
在像 BaseActivity
这样的基础 class 上使用 @ContributesAndroidInjector
形成子组件可能不是一个好的方法。就像 David Medenjak 的评论一样,基础 class 的子组件将被忽略,具体 class 的子组件将在 ABCActivity
上执行注入。
现在,您可以通过在 ABCActivity
:
FragmentManager
来修复错误
@PerActivity
@ContributesAndroidInjector(modules = BaseActivityModule.class)
abstract ABCActivity ABCActivity();