Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在具有抽象片段的MVP体系结构中,如果没有@Providers注释方法,则无法提供片段_Android_Kotlin_Dagger 2 - Fatal编程技术网

Android 在具有抽象片段的MVP体系结构中,如果没有@Providers注释方法,则无法提供片段

Android 在具有抽象片段的MVP体系结构中,如果没有@Providers注释方法,则无法提供片段,android,kotlin,dagger-2,Android,Kotlin,Dagger 2,我知道关于这个问题有很多问题,但我已经浏览了所有的问题并尝试了一整天,但我仍然没有找到解决问题的方法。 我是dagger的新手,但我正在进行的项目正在使用它,在我们有这个案例之前,它一直运行良好: 我们有一个抽象片段,用于注入一个提供者(在MVP体系结构中),还有3个片段扩展了这个片段(在那些体系结构中没有其他注入) 我有以下代码: abstract class Fragment1: DaggerFragment(), Fragment1Contract.View { //region

我知道关于这个问题有很多问题,但我已经浏览了所有的问题并尝试了一整天,但我仍然没有找到解决问题的方法。 我是dagger的新手,但我正在进行的项目正在使用它,在我们有这个案例之前,它一直运行良好:

我们有一个抽象片段,用于注入一个提供者(在MVP体系结构中),还有3个片段扩展了这个片段(在那些体系结构中没有其他注入)

我有以下代码:

abstract class Fragment1: DaggerFragment(), Fragment1Contract.View {

    //region Properties
    @Inject
    lateinit var presenterFragment1Contract: Fragment1Contract.Presenter<Fragment1Contract.View>
最后,这一部分:

@PerApplication
@Component(modules = [ActivityBindingModule::class, AndroidSupportInjectionModule::class, Fragment1Module::class])

interface ApplicationComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        fun build(): ApplicationComponent
    }

    fun inject(app: MyApplication)
}
我尝试了不同的方法,但我有以下错误:

如果没有@Providers注释的方法,则无法提供Fragment1

我尝试添加不同的@Provides方法,但最终我遇到了循环问题,或者Fragment2、3和4或Fragment1Contract缺少注入

我不知道我错过了什么,但在这一点上任何帮助都是有用的

更新:

我试着加上:

    @BindsInstance
    fun view(view: Fragment1): Builder
但现在我有以下错误:

Fragment1 is bound multiple times 
@BindsInstance void dagger.android.AndroidInjector.Builder.seedInstance(T)> @org.jetbrains.annotations.NotNull @BindsInstance androidapp.injection.ApplicationComponent.Builder
injection.ApplicationComponent.Builder.view(Fragment1)

因此,您在此处使用此
Fragment1
,而您的模块不知道此
Fragment1
,因此您需要通过片段中的组件提供此依赖关系

@Provides
internal fun provideFragment1View(fragment: Fragment1): Fragment1Contract.View {
       return fragment
   }
在您的组件中,您必须这样做:

  @BindsInstance
  fun view(view: Fragment1): Builder

谢谢你的回答!我尝试添加@BindsInstance fun视图(视图:Fragment1):Builder,但现在出现以下错误:Fragment1绑定多次org.jetbrains.annotations.NotNull BindsInstance injection.ApplicationComponent.Builder injection.ApplicationComponent.Builder.view(Fragment1)和BindsInstance void dagger.android.androidject.Builder.seedInstance(T)我已经更新了我的答案,使其比注释更清晰。我认为您可以完全删除
ActivityBindingModule
,然后再试一次。我得到了以下崩溃:java.lang.IllegalStateException:OperationFragment必须设置。我认为与您的项目无关。
@Provides
internal fun provideFragment1View(fragment: Fragment1): Fragment1Contract.View {
       return fragment
   }
  @BindsInstance
  fun view(view: Fragment1): Builder