如果没有@Provides注释的方法,则无法提供片段。科特林

时间:2018-10-04 12:25:54

标签: kotlin dagger-2 dagger

我的应用程序课程

class MyApp : Application(), HasActivityInjector {

@Inject
lateinit var activityInjector: DispatchingAndroidInjector<Activity>

/*@Inject
lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment>*/

override fun onCreate() {
    super.onCreate()
    DaggerAppComponent.builder().application(this).build().inject(this)
}

override fun activityInjector(): AndroidInjector<Activity> = 
activityInjector
//override fun supportFragmentInjector(): AndroidSupportInjection<Fragment> 
= fragmentInjector
}

应用组件

@Singleton
@Component(modules = arrayOf(AndroidInjectionModule::class, 
AppModule::class, BuilderModule::class))
interface AppComponent {
@Component.Builder
interface Builder {
    @BindsInstance
    fun application(application: Application): Builder

    fun build(): AppComponent
}

fun inject(app: MyApp)

}

应用模块

@Module
class AppModule {
   @Provides
   @Singleton
   fun provideUtil(application: Application): Utils = Utils(application)
}

Builder Module

@Module
abstract class BuilderModule {
   @ContributesAndroidInjector
   abstract fun contributeMainActivity(): MainActivity

   @ContributesAndroidInjector
   abstract fun contributeHomeFragment(): HomeFragment
}

主要活动

class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {

   @Inject
   lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment>

   override fun supportFragmentInjector(): AndroidInjector<Fragment> = 
   fragmentInjector


   override fun onCreate(savedInstanceState: Bundle?) {
       AndroidInjection.inject(this); // Call before super!
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       replaceFragment(HomeFragment())
    }

    fun replaceFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frameContainer, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }
}

家庭片段

class HomeFragment : Fragment() {

   @Inject
   lateinit var utils: Utils

   override fun onAttach(context: Context?) {
      AndroidSupportInjection.inject(this) // Providing the dependency
      super.onAttach(context)
   }

   override fun onCreateView(inflater: LayoutInflater, container: 
   ViewGroup?, savedInstanceState: Bundle?): View? {
     // Inflate the layout for this fragment
     return inflater.inflate(R.layout.fragment_home, container, false)
   }

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      utils.toaster("Injected")
   }
  

我收到此错误     D:\ Workspace \ DaggerKotlin \ app \ build \ tmp \ kapt3 \ stubs \ debug \ com \ cvsingh \ daggerkotlin \ di \ AppComponent.java:8:错误:[Dagger / MissingBinding] [dagger.android.AndroidInjector.inject(T )]如果没有@Provides注释的方法,则无法提供java.util.Map >>。   公共抽象接口AppComponent {                   ^         java.util.Map >>被注入             dagger.android.DispatchingAndroidInjector。(…,injectorFactoriesWithStringKeys)         dagger.android.DispatchingAndroidInjector被注入             com.cvsingh.daggerkotlin.ui.MainActivity.fragmentInjector         com.cvsingh.daggerkotlin.ui.MainActivity在以下位置注入             dagger.android.AndroidInjector.inject(T)     组件路径:com.cvsingh.daggerkotlin.di.AppComponent? com.cvsingh.daggerkotlin.di.BuilderModule_ContributeMainActivity.MainActivitySubcomponent

}

1 个答案:

答案 0 :(得分:0)

您在MyApp类上做错了,应该使用DaggerApplication简化匕首集成:

class MyApp : DaggerApplication() {
    private val applicationInjector = DaggerApplicationComponent.builder()
        .application(this)
        .build()

    override fun applicationInjector() = applicationInjector
}

对于您的信息,您可以使用DaggerAppCompatActivityDaggerFragment简化您的活动和片段的方式

例如,您的活动可能是:

class MainActivity : DaggerAppCompatActivity(){

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       replaceFragment(HomeFragment())
    }

    fun replaceFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frameContainer, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }
}

如果您不想使用DaggerApplicationDaggerAppCompatActivity等(有时需要从另一个类扩展),则只需复制DaggerApplication,{{ 1}},等等。