Android 通过生成器注入的刀柄适配器为空

Android 通过生成器注入的刀柄适配器为空,android,android-fragments,dependency-injection,dagger-hilt,Android,Android Fragments,Dependency Injection,Dagger Hilt,我不熟悉dagger and hilt,我正试图使用hilt让适配器通过构建器注入到片段中(片段实际上在片段寻呼机适配器中,因此它和适配器有多个实例),它似乎设置正确,我在onCreate中创建super之前构建了组件,但在片段中稍后尝试访问它时适配器为空,我想知道是否需要稍后以某种方式提供它,例如,我的片段中有我的适配器 @AndroidEntryPoint public class CardHolderFragment extends Fragment { public CardA

我不熟悉dagger and hilt,我正试图使用hilt让适配器通过构建器注入到片段中(片段实际上在片段寻呼机适配器中,因此它和适配器有多个实例),它似乎设置正确,我在onCreate中创建super之前构建了组件,但在片段中稍后尝试访问它时适配器为空,我想知道是否需要稍后以某种方式提供它,例如,我的片段中有我的适配器

@AndroidEntryPoint
public class CardHolderFragment extends Fragment {

    public CardAdapter cardAdapter;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    DaggerCardAdapterComponent.builder()
            .cardAdapterDependencies(() -> layoutIdentifier)
            .build().inject(this);
    super.onCreate(savedInstanceState);
    cardAdapter.notifyDataSetChanged(); // CardAdapter.notifyDataSetChanged()' on a null object reference
因此,我的问题是,如果组件设置正确,那么这是可行的还是我遗漏了一个步骤? 我想做的是像cardAdapter=DaggerCardAdapterComponent.getCardAdapter这样的事情,但是我不能在一个组件中加入一个提供方法,我浏览了dagger迁移指南,该指南对我来说有点复杂,因为在切换到hilt之前,我只有几周的时间使用dagger,但是该指南提到

最后,如果为不同的活动或片段组件进行了不同的配置,您可能需要重新设计一些东西。例如,您可以在活动上使用新接口来提供对象

所以我尝试了这个方法,它让我实现了接口,然后返回了一个卡适配器,但我不确定我应该返回什么,欢迎任何帮助

这是我的卡适配器组件

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

interface HasCardAdapter{
    CardAdapter getCardAdapter();
}
} 
这是我的卡适配器构造函数

@Inject
public CardAdapter(
        ItemTouchListener onItemTouchListener,
        String layoutIdentifier,
        TypeFactory typeFactory,
        RequestManager glide
) {
    this.onItemTouchListener = onItemTouchListener;
    this.typeFactory = typeFactory;
    this.layoutIdentifier = layoutIdentifier;
    this.glide = glide;
    this.elements = new ArrayList<>();
}
@Inject
公共卡适配器(
ItemTouchListener和MTouchListener,
字符串布局标识符,
类型工厂类型工厂,
请求管理器滑动
) {
this.onItemTouchListener=onItemTouchListener;
this.typeFactory=typeFactory;
this.layoutIdentifier=layoutIdentifier;
this.glide=glide;
this.elements=新的ArrayList();
}

首先,如果您正在构建依赖组件,您不需要使用
@AndroidEntryPoint
,即使在我的情况下,在动态功能模块中使用
@AndroidEntryPoint
导致它无法编译

编辑:在您的情况下,构建从属构件或使用生成器构建构件看起来没有必要。如果是这样,你可以使用下面的答案将有帮助,如果你不需要依赖组件,我会添加另一个编辑来做它的简单方式

您应该使用
EntryPointAccessors.fromApplication()
EntryPointAccessors.fromActivity()
EntryPointAccessors.fromFragment()
来创建一个接口,该接口具有配置方法,带有
@InstallIn
@EntryPoint
注释。我解释了如何使用Hilt构建依赖组件和动态功能模块,您可以找到

首先,使用您提供的依赖项创建一个模块:

@Module
@InstallIn(ApplicationComponent::class)
class CoreModule {

    @Singleton
    @Provides
    fun provideCoreDependency(application: Application) = CoreDependency(application)

    @Provides
    fun provideCoreActivityDependency(context: Application) = CoreActivityDependency(context)
}
根据您的结构,您可以将
ApplicationComponent
更改为
ActivityComponent
FragmentComponent

然后,使用
@EntryPoint
@InstallIn
注释和配置方法构建依赖项接口,这就是向依赖组件提供依赖项的方式

@EntryPoint
@InstallIn(ApplicationComponent::class)
interface CoreDependencies {

    /*
     * Provision methods to provide dependencies to components that
     * depend on this component
     */
    fun coreDependency(): CoreDependency

    fun coreActivityDependency(): CoreActivityDependency

}
现在构建您应该创建的组件,它是为您准备的
CardAdapterComponent

@Component(
        dependencies = [CoreDependencies::class],
        modules = [CameraModule::class]
)
interface CameraComponent {

    fun inject(cameraFragment1: CameraFragment1)
    fun inject(cameraFragment2: CameraFragment2)


    fun inject(cameraActivity: CameraActivity)

    @Component.Factory
    interface Factory {
        fun create(coreDependencies: CoreDependencies, @BindsInstance application: Application): CameraComponent
    }

}
我用的是工厂模式,如果你愿意可以用builder,两者都一样

要将依赖组件注入
活动
,需要使用
入口点
获取组件

private fun initHiltDependencyInjection() {

    val coreComponentDependencies = EntryPointAccessors.fromApplication(
            applicationContext,
            CoreDependencies::class.java
    )

    DaggerCameraComponent.factory().create(
            coreComponentDependencies,
            application
    )
            .inject(this)


}
要注入到
片段

private fun initCoreDependentInjection() {

    val coreComponentDependencies = EntryPointAccessors.fromApplication(
            requireActivity().applicationContext,
            CoreDependencies::class.java
    )

    DaggerCameraComponent.factory().create(
            coreComponentDependencies,
            requireActivity().application
    )
            .inject(this)

}

适配器应标记为
@Inject
@Inject public CardAdapter cardapter
。要使用刀柄,您应该使用
@AndroidEntryPoint
标记片段以及容器活动,然后移除手动组件注入。(我假设您的模块配置正确)我会尽快尝试,让您知道您的
Fragment
an
androidx.Fragment
?它是一个androidx。Fragment@Pavneet_Singh你知道我在这里用的是建筑商吗?因为它需要运行时的辩论,给你赏金作为唯一严肃的回答,lol会尝试并接受它,如果它有帮助的话,很多感谢在我提供的链接中,你可以查看匕首柄的基本知识。它会帮助你的。当您的片段在动态功能模块中或在另一个没有@AnroidEntryPoint的对象中无法访问应用程序组件时,您需要使用依赖组件或构建器,等等。我建议你先检查一下github链接,看看简单的匕首刀柄注射是如何工作的,我想它会对你有用的。你也可以检查我的问题,看看结构。