@Binds方法必须只有一个参数,其类型可以分配给返回类型 [英] @Binds methods must have only one parameter whose type is assignable to the return type
问题描述
我正在迁移到新的dagger android 2.11
I am migrating to the new dagger android 2.11
所有设置都基于Google蓝图:MVP-Dagger。但是我遇到了这个错误:
All set up based on the Google blueprint :MVP-Dagger.but I am having this error :
错误:(22,57)错误:@Binds方法必须只有一个参数
,其类型可分配给返回类型
Error:(22, 57) error: @Binds methods must have only one parameter whose type is assignable to the return type
此行:
@ActivityScoped
@Binds abstract PresenterFactory<MainContract.Presenter> providePresenterFactory(MainPresenter presenter);
主持人:
@ActivityScoped
public class MainPresenter extends BasePresenterImpl<MainContract.View>
implements MainContract.Presenter { public MainPresenter(String s) {..
} ... }
有人对如何解决这个问题有任何想法吗?谢谢。
Someone have any idea on how to solve this? Thanks.
推荐答案
错误消息解释了所有内容:
The error message explains everything:
@Binds方法必须只有一个参数,其类型可以分配给返回类型
@Binds methods must have only one parameter whose type is assignable to the return type
您的 @Binds
方法的参数为 MainPresenter
。这不能分配给返回类型 PresenterFactory< MainContract.Presenter>
。换句话说, MainPresenter
不是 PresenterFactory< MainContract.Presenter>
的实例。
Your @Binds
method has a parameter of MainPresenter
. This is not assignable to the return type PresenterFactory<MainContract.Presenter>
. In other words, MainPresenter
is not an instance of PresenterFactory<MainContract.Presenter>
.
@Binds
方法的正确语法如下:
The correct syntax for @Binds
methods is something like:
@Binds
abstract Abstraction bindAbstration(Concretion concretion)
其中 concretion
是 Abstraction
的实例。
或者,在Kotlin中:
Or, in Kotlin:
@Binds
abstract fun bindAbstraction(concretion: Concretion) : Abstraction
@Binds
方法不是魔术。它们绑定了Dagger已经知道如何提供的类型(例如接口)和该类型的实现。
@Binds
methods are not magic. They bind a type (for instance, an interface) and an implementation of that type that Dagger knows how to provide already.
更新
您可以转换 @Provides
到 @Binds
的步骤如下:
You can convert your @Provides
to @Binds
with the following steps:
- 确保
MainPresenter
有一个显式的构造函数,该构造函数带有@Inject
注释,并且Dagger 2可以在构造函数中提供依赖项。 / li>
-
写入:
- Make sure
MainPresenter
has an explicit constructor annotated with@Inject
and that Dagger 2 can provide the dependencies in the constructor. Write:
@Binds
abstract MainContract.Presenter bindPresenter(MainPresenter mainPresenter);
这篇关于@Binds方法必须只有一个参数,其类型可以分配给返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!