android - Dagger 2 - 提供依赖时出错
问题描述
我对 Dagger 2 真的很陌生,我知道它是如何工作的以及它做了什么,但是我在尝试将它实施到我的项目中时遇到了一些问题。
我现在的目标是将演示者注入我的视图中,目标是解耦我的做事视图
presenter = Presenter(myInteractor())
这是我尝试过的
我的应用程序
class MyAppApplication: Application() {
lateinit var presentationComponent: PresentationComponent
override fun onCreate() {
super.onCreate()
createPresentationComponent()
}
private fun createPresentationComponent() {
presentationComponent = DaggerPresentationComponent.builder()
.presentationModule(PresentationModule(this))
.build()
}
}
演示组件
@Component(modules = arrayOf(PresentationModule::class))
@Singleton
interface PresentationComponent {
fun inject(loginActivity: LoginView)
fun loginUserPresenter(): LoginPresenter
}
演示模块
@Module
class PresentationModule(application: Application) {
@Provides @Singleton fun provideLoginUserPresenter(signInInteractor: SignInInteractor): LoginPresenter {
return LoginPresenter(signInInteractor)
}
}
登录交互器
interface SignInInteractor {
interface SignInCallBack {
fun onSuccess()
fun onFailure(errormsg:String)
}
fun signInWithEmailAndPassword(email:String,password:String,listener:SignInCallBack)
fun firebaseAuthWithGoogle(account: GoogleSignInAccount, listener:SignInCallBack)
}
现在,我认为这就是我需要将交互器注入我的演示者而没有任何问题然后将演示者注入我的视图中所需的全部内容,但是给了我这个错误
error: [Dagger/MissingBinding] com.myapp.domain.interactor.logininteractor.SignInInteractor cannot be provided without an @Provides-annotated method.
我有点困惑,因为如果我只提供负责将我的 signInInteractor 绑定到我的 Presenter 的presentationModule,它应该可以工作,但不是。
提前感谢您的帮助
解决方案
正如错误消息所说,您正在尝试将 a 传递给 your SignInInteractor
,但您没有在任何地方为其提供实现。一个可能的解决方案是将以下代码块添加到您的:PresentationModule
LoginPresenter
PresentationModule
@Provides @Singleton fun provideSignInInteractor(): SignInInteractor {
return TODO("Add an implementation of SignInInteractor here.")
}
当然,TODO
需要用SignInInteractor
您选择的 a 替换(myInteractor()
例如,该功能可以工作)。然后,SignInInteractor
您的LoginPresenter
. 希望有帮助!
推荐阅读
- python-2.7 - 如何修复新的 chrome 驱动程序的异常 - WebDriverException:消息:无效参数:值必须是非负整数
- haskell - 如何在 Haskell 源文件中包含多行编号?
- javascript - 用一个表连接 3 个表
- git - 致命:无法读取“https://OrganizationName@dev.azure.com”的密码:终端提示已禁用
- android - Kotlin: unresolved reference for Updating UI using Observables
- google-apps-script - 用静态值替换公式
- java - 定时器启动后可切换按钮不起作用
- group-by - Salesforce SOQL GROUP BY 是否区分不同区域设置的大小写?
- java - Does Jackson deserialization have a maximum for inhertiance depth?
- node.js - 启动 nodejs api 应用程序时的偏移错误