Kotlin 参数类型不匹配
Kotlin parameter type mismatch
我正在尝试将以下 Java 代码转换为 Kotlin。它编译并工作正常。
public abstract class MvpViewHolder<P extends BasePresenter> extends RecyclerView.ViewHolder {
protected P presenter;
public MvpViewHolder(View itemView) {
super(itemView);
}
public void bindPresenter(P presenter) {
this.presenter = presenter;
presenter.bindView(this);
}
public void unbindPresenter() {
presenter = null;
}
}
在我目前的代码中,我在 presenter.bindView(this)
上收到一个错误,指出 Required: Nothing, Found: MvpViewHolder
。
abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<*,*> {
protected var presenter: P? = null
fun bindPresenter(presenter: P): Unit {
this.presenter = presenter
//I get the error here
presenter.bindView(this)
}
fun unbindPresenter(): Unit {
presenter = null
}
}
bindView
是这样定义的
public abstract class BasePresenter<M,V> {
fun bindView(view: V) {
this.view = WeakReference(view)
}
}
我现在唯一可以归咎于没有正确定义 class 泛型。据我所知,this
仍然是预期作为参数的 View 泛型的正确实例,我也绝对不明白它怎么会是 Nothing
。我该如何修复该错误?
编辑:Java BasePresenter 代码
public abstract class BasePresenter<M, V> {
protected M model;
private WeakReference<V> view;
public void bindView(@NonNull V view) {
this.view = new WeakReference<>(view);
if (setupDone()) {
updateView();
}
}
protected V view() {
if (view == null) {
return null;
} else {
return view.get();
}
}
}
您不能直接使用它来指向当前class。
你需要使用
this@class_name
。
例如,如果“Example
”是 class
名称,您可以使用
this@Example
表示this
在Java
有关详细信息,请访问 https://kotlinlang.org/docs/reference/this-expressions.html
您的 bindView
方法需要一个 View
作为参数。
当您定义一个变量(view
在 BasePresenter 上定义)时,您看到的错误会出现,该变量可以保存空结果,或者在您的情况下,是一个 View 对象。
在下面的代码中,您将 this
绑定为参数并且 MapViewHolder 不是 View 的子类。
abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<*,*> {
protected var presenter: P? = null
fun bindPresenter(presenter: P): Unit {
this.presenter = presenter
//I get the error here
presenter.bindView(this) // -> this references MvpViewHolder which isn't a subclass of View
}
fun unbindPresenter(): Unit {
presenter = null
}
}
我认为您想要的是将 itemView 附加到演示者,因为它实际上是一个 View
对象。
编辑
问题与定义 BasePresenter<*,*>
有关,这意味着在这种情况下 BasePresenter<Nothing, Nothing>
(kotlin 中没有任何对象)-阅读更多关于 star projections 在 this link.
上的科特林中
我建议明确定义基本演示者期望的类型或明确定义为 BasePresenter<Any?, Any?>
。
我正在尝试将以下 Java 代码转换为 Kotlin。它编译并工作正常。
public abstract class MvpViewHolder<P extends BasePresenter> extends RecyclerView.ViewHolder {
protected P presenter;
public MvpViewHolder(View itemView) {
super(itemView);
}
public void bindPresenter(P presenter) {
this.presenter = presenter;
presenter.bindView(this);
}
public void unbindPresenter() {
presenter = null;
}
}
在我目前的代码中,我在 presenter.bindView(this)
上收到一个错误,指出 Required: Nothing, Found: MvpViewHolder
。
abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<*,*> {
protected var presenter: P? = null
fun bindPresenter(presenter: P): Unit {
this.presenter = presenter
//I get the error here
presenter.bindView(this)
}
fun unbindPresenter(): Unit {
presenter = null
}
}
bindView
是这样定义的
public abstract class BasePresenter<M,V> {
fun bindView(view: V) {
this.view = WeakReference(view)
}
}
我现在唯一可以归咎于没有正确定义 class 泛型。据我所知,this
仍然是预期作为参数的 View 泛型的正确实例,我也绝对不明白它怎么会是 Nothing
。我该如何修复该错误?
编辑:Java BasePresenter 代码
public abstract class BasePresenter<M, V> {
protected M model;
private WeakReference<V> view;
public void bindView(@NonNull V view) {
this.view = new WeakReference<>(view);
if (setupDone()) {
updateView();
}
}
protected V view() {
if (view == null) {
return null;
} else {
return view.get();
}
}
}
您不能直接使用它来指向当前class。
你需要使用
this@class_name
。
例如,如果“Example
”是 class
名称,您可以使用
this@Example
表示this
在Java
有关详细信息,请访问 https://kotlinlang.org/docs/reference/this-expressions.html
您的 bindView
方法需要一个 View
作为参数。
当您定义一个变量(view
在 BasePresenter 上定义)时,您看到的错误会出现,该变量可以保存空结果,或者在您的情况下,是一个 View 对象。
在下面的代码中,您将 this
绑定为参数并且 MapViewHolder 不是 View 的子类。
abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<*,*> {
protected var presenter: P? = null
fun bindPresenter(presenter: P): Unit {
this.presenter = presenter
//I get the error here
presenter.bindView(this) // -> this references MvpViewHolder which isn't a subclass of View
}
fun unbindPresenter(): Unit {
presenter = null
}
}
我认为您想要的是将 itemView 附加到演示者,因为它实际上是一个 View
对象。
编辑
问题与定义 BasePresenter<*,*>
有关,这意味着在这种情况下 BasePresenter<Nothing, Nothing>
(kotlin 中没有任何对象)-阅读更多关于 star projections 在 this link.
我建议明确定义基本演示者期望的类型或明确定义为 BasePresenter<Any?, Any?>
。