0

I really need help with solving the following error:

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]

I tried this approach but for some reason, this does not work for me, and here are my classes:

@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;

    ...

So as you can see I implemented a similar logic as proposed in the link provided above, but for some reason that is not working. Please, help me to understand what's wrong...

2 Answers2

1

I added an empty constructor annotated with @Inject for the Fragment and it worked... Don't completely understand why Activity injection works without it and Fragment injection not but the problem is solved

0

If you have ApplicationScope > ActivityScope > FragmentScope then your Activity (ActivityScope) can't directly inject a Fragment (FragmentScope), because the Fragment isn't part of the Activity's graph, the same way you can't inject that Activity in your Application. Subcomponents can inject objects available on the parent component, but not the other way around.

The simplest solution would be to not inject the Fragment and just create it like you usually would. You can inject the Activity into the Fragment (parent > child scope) and use any other Activity / Fragment scoped objects there without further problems.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • well, to test this idea I've commented every scope annotation everywhere and got exactly the same build error.. – Pavlo Kuchereshko Aug 15 '19 at 00:09
  • @PavloKuchereshko I mean..of course. You [don't provide `FragmentA` or make it accessible for constructor injection](https://stackoverflow.com/a/44912081/1837367), at least not in the code you included – David Medenjak Aug 15 '19 at 08:23
  • ok so I've added an empty constructor with @Inject annotation into the Fragment and it worked, but it also works if I remove the `FragmentsModule` as well. help me please to understand why the `ActivitiesModule` with the abstract methods marked with `@ContributesAndroidInjector` and without `@Inject` constructors in Activities is working fine and the `FragmentModule`, made in the same way, is not working? – Pavlo Kuchereshko Aug 15 '19 at 11:36