Dagger2 中缺少绑定?
我制作了一个简单的应用程序,在 dagger2 的帮助下告诉汽车是两轮车还是四轮车,但是如果我运行该应用程序,就会出现错误,并且错误是绑定丢失,为此我还使用了 @Named 注释但错误一次又一次地出现。
MainActivity.kt
class MainActivity : AppCompatActivity() {
@Inject
lateinit var car: Car
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
DaggerCarComp.builder().build().inject(this)
car.tellCarTypes()
}
}
Car.kt
class Car @Inject constructor(
@Named("two") val tw: TW,
val fw: FW
) {
fun tellCarTypes() {
tw.whichCar()
}
}
CarComp.kt
@Component(modules = [CarModule::class])
interface CarComp {
fun inject (mainActivity: MainActivity)
}
CarType.kt
interface CarType {
fun whichCar()
}
class TW @Inject constructor() : CarType {
override fun whichCar() {
Log.d("Type", "whichCar: Two wheeler")
}
}
class FW : CarType {
override fun whichCar() {
Log.d("Type", "whichCar: Four wheeler")
}
}
CarModule.kt
@Module
class CarModule {
@Provides
@Named("two")
fun tellCar(tw: TW) : CarType {
return tw
}
@Provides
@Named("four")
fun tellCar2() : CarType {
return FW()
}
}
I have made a simple app that tells the car is a two-wheeler or four-wheeler with the help of dagger2 but an error will occur if I run the app and the error is binding is missing for this I have also used @Named annotation but the error is coming again and again.
MainActivity.kt
class MainActivity : AppCompatActivity() {
@Inject
lateinit var car: Car
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
DaggerCarComp.builder().build().inject(this)
car.tellCarTypes()
}
}
Car.kt
class Car @Inject constructor(
@Named("two") val tw: TW,
val fw: FW
) {
fun tellCarTypes() {
tw.whichCar()
}
}
CarComp.kt
@Component(modules = [CarModule::class])
interface CarComp {
fun inject (mainActivity: MainActivity)
}
CarType.kt
interface CarType {
fun whichCar()
}
class TW @Inject constructor() : CarType {
override fun whichCar() {
Log.d("Type", "whichCar: Two wheeler")
}
}
class FW : CarType {
override fun whichCar() {
Log.d("Type", "whichCar: Four wheeler")
}
}
CarModule.kt
@Module
class CarModule {
@Provides
@Named("two")
fun tellCar(tw: TW) : CarType {
return tw
}
@Provides
@Named("four")
fun tellCar2() : CarType {
return FW()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的类 FW 的构造函数旁边没有 @Inject 注释。我认为它必须以这种方式定义,以便 Dagger 知道要做什么
另外,您的函数
tellCar2
可能应该以这种方式定义:由于您使用的是接口,所以您也可以在抽象中使用 @Binds模块,因此您可以根据您的需求直接绑定接口的正确实现。查看这篇文章:https://medium. com/mobile-app-development-publication/dagger-2-binds-vs-provides-cbf3c10511b5 或此:https://dagger.dev/api/2.21/dagger/Binds.html
Your class FW does not have the @Inject annotation alongside his constructor. I think it has to be defined this way so Dagger know what to do
Also your function
tellCar2
should probably be defined in this way :Since you are using an interface, you could also use @Binds in an abstract module and so you can directly bind the correct implementation of the interface regarding your needs. Check this article : https://medium.com/mobile-app-development-publication/dagger-2-binds-vs-provides-cbf3c10511b5 or this : https://dagger.dev/api/2.21/dagger/Binds.html