2

我是 Hilt(和匕首)的新手,我不确定标题是否真的是我的问题,但它是一个起点,

我正在尝试在回收器视图适配器上使用 hilt,它需要 4 个东西:一个接口、glide(图像加载器)、一个我拥有的类型类和一个字符串标识符,我为它设置了组件和模块并添加了一个标识符的生成器,这似乎可以工作,因为我可以注释适配器构造函数并且它可以编译,但是我需要访问我的片段中的适配器,所以我想我可以用 @Inject 注释适配器字段但是当我添加它时失败(下面的错误),所以这是我的片段的片段

@AndroidEntryPoint
public class CardHolderFragment extends Fragment implements ItemTouchListener, 
SwipeRefreshLayout.OnRefreshListener {

@Inject
public CardAdapter cardAdapter;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {

    DaggerCardAdapterComponent.builder()
            .cardAdapterDependencies(() -> layoutIdentifier)
            .build()
            .inject(this);

    DaggerSentenceViewModelComponent.builder()
            .context(getActivity())
            .appDependencies(() -> args)
            .build()
            .inject(this);

我得到的错误是

/Users/martinseal/StudioProjects/SimpleAAC/app/src/main/java/com/xxx/SentenceViewModelComponent.java:14: 
error: [Dagger/MissingBinding] 
com.xxx.ItemTouchListener cannot be provided without an @Provides-annotated method.
    public interface SentenceViewModelComponent {
           ^
          com.xxx.ItemTouchListener is injected at
              com.xxx.CardAdapter(onItemTouchListener, …)
          com.xxx.CardAdapter is injected at
              com.xxx.CardHolderFragment.cardAdapter
          com.xxx.CardHolderFragment is injected at

com.xxx.SentenceViewModelComponent.inject(com.xxx.CardHolderFragment)
/Users/martinseal/StudioProjects/SimpleAAC/app/src/main/java/
com/xxx/CardAdapterComponent.java:13: error: 

[Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.

所以我可以看到它告诉我它没有为 SentenceViewModelComponent 和 CardAdapterComponent 的 ItemTouchListener 提供方法(我确实添加但无济于事)存在问题,但为什么它们会在这个依赖图中相关?它们都被注入到同一个 CardHolderFragment 中,这是我的 CardAdapterComponent:

@Singleton
@Component(dependencies = CardAdapterDependencies.class, modules = {TypeFactoryModule.class, ItemTouchListenerModule.class, GlideModule.class})

public interface CardAdapterComponent {

    void inject(CardHolderFragment cardHolderFragment);

    @Component.Builder
    interface Builder {
        Builder cardAdapterDependencies(CardAdapterDependencies cardAdapterDependencies);

        CardAdapterComponent build();
    }
}

如您所见,它依赖于某些模块的第一类工厂

@Module
@InstallIn(ActivityComponent.class)
public abstract class TypeFactoryModule {

    @Binds
    abstract TypeFactory bindTypeFactory(TypeFactoryForList typeFactoryForList);
}

然后项目触摸监听器

@Module
@InstallIn(ActivityComponent.class)
public abstract class ItemTouchListenerModule {
    @Binds
    abstract ItemTouchListener bindItemTouchListener(CardHolderFragment cardHolderFragment);
}

然后滑行

@Module
@InstallIn(ApplicationComponent.class)
public class GlideModule {

    @Singleton
    @Provides
    public static RequestManager provideGlideRequestManager(@ApplicationContext Context context) {
        return Glide.with(context);
    }
}

然后是组件的依赖项

@EntryPoint
@InstallIn(ApplicationComponent.class)
public interface CardAdapterDependencies {

    @LayoutIdentifier String layoutIdentifier();

}

LayoutIdentifier 是一个具有 RUNTIME 的 RetentionPolicy 的限定符,它限定了我的布局标识符模块

@Module
@InstallIn(ApplicationComponent.class)
public class LayoutIdentifierModule {

    @Singleton
    @Provides
    @LayoutIdentifier
    public String provideLayoutIdentifier(){ return Constants.MAIN_IDENTIFIER; };

}

我的 SentenceViewModelComponent 不依赖任何这些

@Singleton
@Component(dependencies = SentenceViewModelDependencies.class)
public interface SentenceViewModelComponent {

    void inject(CardHolderFragment cardHolderFragment);


    @Component.Builder
    interface Builder {
        Builder context(@BindsInstance Context context);
        Builder appDependencies(SentenceViewModelDependencies sentenceViewModelDependencies);
        SentenceViewModelComponent build();
    }

}

但确实依赖于 SentenceViewModelDependencies

@EntryPoint
@InstallIn(ApplicationComponent.class)
public interface SentenceViewModelDependencies {

    @SentenceTypeAndDescription String[] sentenceTypeAndDescription();

}

它有一个称为 SentenceTypeAndDescription 的限定符,它注释一个模块 SentenceTypeAndDescriptionModule

@Module
@InstallIn(ApplicationComponent.class)
public class SentenceTypeAndDescriptionModule {

    @Singleton
    @Provides
    @SentenceTypeAndDescription
    public String[] provideSentenceTypeAndDescription(){ return new String[]{Constants.QUICKS, "PRONOUNS"}; };

}

除了它们都注入了相同的片段之外,我看不到错误是如何相关的,我应该在这里使用的子组件周围是否缺少一些东西?任何帮助,将不胜感激

4

0 回答 0