Dagger2 中缺少绑定?

发布于 2025-01-16 14:31:02 字数 1468 浏览 0 评论 0原文

我制作了一个简单的应用程序,在 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()
    }
}

Error image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2025-01-23 14:31:02

您的类 FW 的构造函数旁边没有 @Inject 注释。我认为它必须以这种方式定义,以便 Dagger 知道要做什么


class FW @Inject constructor() : CarType {
    override fun whichCar() {
        Log.d("Type", "whichCar: Four wheeler")
    }
}

另外,您的函数 tellCar2 可能应该以这种方式定义:

@Provides
@Named("four"
fun tellCar2(fw: FW) : CartType {
   return fw
}

由于您使用的是接口,所以您也可以在抽象中使用 @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


class FW @Inject constructor() : CarType {
    override fun whichCar() {
        Log.d("Type", "whichCar: Four wheeler")
    }
}

Also your function tellCar2 should probably be defined in this way :

@Provides
@Named("four"
fun tellCar2(fw: FW) : CartType {
   return fw
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文