MainComponent 中的 MissingBinding 对象在子组件的模块中提供

MissingBinding object in MainComponent which is provided in module in subcompnent

我使用具有 ActivityBuilderModule 和 ViewModelBuilderModule 的“AppComponent”以及具有提供 InterfaceRequest 的 NetworkModule 的子组件“NetworkComponent”,我在 Repository 中注入 InterfaceRequest,在 ViewModel 中注入 Repository,ViewModel 注入在 Activity 但我得到了这个错误:

[Dagger/MissingBinding] InterfaceRequests cannot be provided without an @Provides-annotated method.
public interface AppComponent {
  A binding with matching key exists in component: NetworkComponent
      InterfaceRequests is injected at
          ForYouRepository.interfaceRequests
      ForYouRepositoryis injected at
          NewsViewModel(newsRepository)
      NewsViewModel is injected at
          ViewModelBuilderModule.bindNewsViewModel(newsViewModel)
      java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
          ViewModelFactory(creators)
      tech.gplanet.shopx.di.ViewModelFactory is injected at
          tech.gplanet.shopx.ui.activities.NewsActivity.viewModelFactory
      tech.gplanet.shopx.ui.activities.NewsActivity

应用程序组件:

@Singleton
@Component(modules = { AndroidInjectionModule.class, ActivityBuilderModule.class, ViewModelBuilderModule.class})
public interface AppComponent {

    void inject(MyApplication appClass);


    @Component.Builder
    interface Builder {

        @BindsInstance
        AppComponent.Builder context(Context context);
        
        AppComponent build();

    }
    NetworkComponent.Builder getNetworkComponentBuilder();
}

网络组件:

@Subcomponent(modules = {NetworkModule.class})
public interface NetworkComponent {

    void inject(RegisterationActivity registerationActivity);
    void inject(AuthenticationRepository repository);
    void inject(ForYouRepository repository);

    @Subcomponent.Builder
    interface Builder {

        @BindsInstance
        Builder customDeserializers(@Named("custom_deserializers") GeneralCustomDeserializerInterface[] deserializers);

        @BindsInstance
        @Nullable
        Builder baseUrl(@Named("base_url") String baseUrl);

        NetworkComponent build();
    }
}

存储库代码:

  @Inject
    public InterfaceRequests interfaceRequests;


    @Inject
    public ForYouRepository(Context context) {
        NetworkComponent networkComponent = ((MyApplication)((Activity) context).getApplication()).getAppComponent()
                .getNetworkComponentBuilder()
                .baseUrl(Constants.BASE_URL)
                .customDeserializers(new GeneralCustomDeserializerInterface[]{})
                .build();

        networkComponent.inject(this);
    }

网络模块:

    @Provides
    public Gson provideGsonBuilder(@Named("custom_deserializers") GeneralCustomDeserializerInterface [] customDeserializersList) {

        GsonBuilder gsonBuilder = new GsonBuilder();

        if(customDeserializersList != null) {
            for(GeneralCustomDeserializerInterface customDeserializer : customDeserializersList)
                gsonBuilder.registerTypeAdapter(customDeserializer.getDeserializedClass(), customDeserializer);
        }

        return gsonBuilder.create();
    }

    @Provides
    public Retrofit provideRetrofitInstance(@Named("base_url") String baseURL,  Gson gson) {
        return new Retrofit.Builder()
                .baseUrl(baseURL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                //.callFactory(okHttpClient)
                .build();
    }

    @Provides
    public InterfaceRequests provideInterfaceRequests(Retrofit retrofit) {
        return retrofit.create(InterfaceRequests.class);
    }

我想你误解了子组件。 子组件中的对象不能被组件使用。关系只是方向相反(子组件可以使用主组件的对象)

我认为你应该在 AppComponent 中使用网络模块