简体   繁体   English

在子组件的模块中提供的 MainComponent 中缺少 object

[英]MissingBinding object in MainComponent which is provided in module in subcompnent

I use "AppComponent" which has ActivityBuilderModule and ViewModelBuilderModule and SubComponent "NetworkComponent" which has NetworkModule which provide InterfaceRequest and I inject InterfaceRequest in Repository and inject Repository in ViewModel and the ViewModel is injected in Activity but I got this error:我使用具有 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

AppComponent:应用组件:

@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();
}

Network Component:网络组件:

@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();
    }
}

Repository code:存储库代码:

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

Network Module:网络模块:

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

I think you misunderstood subcomponent.我认为您误解了子组件。 Object in a subcomponent can't be used by the component.子组件中的 Object 不能被组件使用。 The relation is only in the opposite direction (subcomponent can use object from the main component)关系只是相反的方向(子组件可以使用来自主组件的 object)

I think in your case you should use a network module in AppComponent我认为在您的情况下,您应该在 AppComponent 中使用网络模块

暂无
暂无

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

相关问题 Dagger-Hilt:[Dagger/MissingBinding] Object 如果没有 @Inject 构造函数或 @Provides 注释方法,则无法提供来自外部模块 - Dagger-Hilt: [Dagger/MissingBinding] Object from external Module cannot be provided without an @Inject constructor or an @Provides-annotated method Dagger/MissingBinding 但模块存在 - Dagger/MissingBinding but module exists 没有@Provides-annotated 方法就不能提供 Dagger/MissingBinding Repository - Dagger/MissingBinding Repository cannot be provided without an @Provides-annotated method 如果没有@Provides 注释的方法 Dagger/MissingBinding,则无法提供 - cannot be provided without an @Provides-annotated method Dagger/MissingBinding 如果没有@Provides注释的方法,则无法提供Dagger / MissingBinding - Dagger/MissingBinding cannot be provided without an @Provides-annotated method 无法通过 HiltViewModel 中的构造函数传递字符串 - 错误:[Dagger/MissingBinding] java.lang.String 无法提供 - Can't Pass Strings Through Constructor in HiltViewModel - Error: [Dagger/MissingBinding] java.lang.String cannot be provided 错误:[Dagger/MissingBinding] 地图<Class<? extends ViewModel> , 提供者<ViewModel> &gt; 如果没有@Provides 注释的方法,则无法提供 - error: [Dagger/MissingBinding] Map<Class<? extends ViewModel>, Provider<ViewModel>> cannot be provided without an @Provides-annotated method 匕首-MissingBinding地图 <Class<? extends ViewModel> ,供应商 <ViewModel> &gt;如果没有@Provides注释的方法,则无法提供 - Dagger - MissingBinding Map<Class<? extends ViewModel>,Provider<ViewModel>> cannot be provided without an @Provides-annotated method [Dagger/MissingBinding].Kotlinjvm.functions.Function1<!--? super java.lang.Integer.Unit--> 无法提供 - [Dagger/MissingBinding].Kotlinjvm.functions.Function1<? super java.lang.Integer.Unit> cannot be provided 错误:[Dagger/MissingBinding] *.AuthRepository 不能在没有 @Provides 注释的方法的情况下提供 - error: [Dagger/MissingBinding] *.AuthRepository cannot be provided without an @Provides-annotated method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM