Dagger 2将片段注入活动-错误:[Dagger / MissingBinding]

时间:2019-08-14 00:54:37

标签: android dependency-injection dagger-2

我真的需要帮助解决以下错误:

error: [Dagger/MissingBinding] prj.view.fragment.FragmentA cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
A binding with matching key exists in component: prj.di.module.FragmentsModule_ContributeFragmentA.FragmentASubcomponent
prj.view.fragment.FragmentA is injected at
prj.view.activity.MainActivity.fragmentA
prj.view.activity.MainActivity is injected at
dagger.android.AndroidInjector.inject(T) [prj.di.component.ApplicationComponent → prj.di.module.ActivitiesModule_ContributeMainActivity.MainActivitySubcomponent]

我尝试了approach,但是由于某种原因,这对我不起作用,这是我的课程:

@ApplicationScope
@Component(modules = {ApplicationContextModule.class, RetrofitModule.class, ActivitiesModule.class, AndroidSupportInjectionModule.class})
public interface ApplicationComponent extends AndroidInjector<MyApplication> {

    @Component.Factory
    interface Factory extends AndroidInjector.Factory<MyApplication> {}
}
@Module
public abstract class ApplicationContextModule {

    @Binds
    @ApplicationScope
    @ApplicationContext
    abstract Context bindsContext(MyApplication context);
}
@Module
public abstract class ActivitiesModule {

    @ActivityScope
    @ContributesAndroidInjector(modules = {MainActivityContextModule.class, FragmentsModule.class})
    abstract MainActivity contributeMainActivity();
}
@Module
public class MainActivityContextModule {

    @Provides
    @ActivityScope
    @ActivityContext
    Context provideContext(MainActivity context){
        return context;
    }
}
@Module
public abstract class FragmentsModule {

    @FragmentScope
    @ContributesAndroidInjector()
    abstract FragmentA contributeFragmentA();
}
// BaseActivity extends DaggerAppCompatActivity
public class MainActivity extends BaseActivity {

    @Inject FragmentA fragmentA;

    @Inject
    @ApplicationContext
    public Context applicationContext;

    @Inject
    @ActivityContext
    public Context activityContext;

    ...
public class FragmentA extends DaggerFragment {
    @Inject
    DispatchingAndroidInjector<Fragment> childFragmentInjector;

    @Inject
    @ActivityContext
    Context activityContext;

    ...

因此,您可以看到我实现了与上面提供的链接中所提出的逻辑类似的逻辑,但是由于某些原因,该逻辑不起作用。拜托,请帮助我了解问题所在...

2 个答案:

答案 0 :(得分:1)

我为Fragment添加了一个用@Inject注释的空构造函数,它的工作原理...无法完全理解为什么Activity注入在没有它的情况下会起作用,而Fragment注入却无法解决,但问题得以解决

答案 1 :(得分:0)

如果您有' Sheets("DailyProduction").Select Range("A2:I150").Select Selection.copy Sheets("DATA").Select Range("O7").Select ActiveSheet.Paste Sheets("DailyProduction").Select Application.CutCopyMode = False Selection.ClearContents End Sub ''' ,则您的活动(ApplicationScope > ActivityScope > FragmentScope)无法直接注入片段(ActivityScope),因为片段不是活动图的一部分,因此您无法在应用程序中注入该活动的方式。子组件可以注入父组件上可用的对象,反之则不能。

最简单的解决方案是注入片段,然后像通常那样创建它。您可以将Activity插入Fragment(父范围>子范围),并在那里使用任何其他Activity / Fragment范围内的对象,而不会产生更多问题。