【发布时间】:2017-11-01 03:05:22
【问题描述】:
我正在尝试将以下 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)
}
}
我现在唯一可以将其归因于没有正确定义类泛型。据我所知,this 仍然是预期作为参数的 View 泛型的正确实例,我也绝对看不出它怎么可能是 Nothing。如何修复错误?
编辑:BasePresenter 的 Java 代码
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();
}
}
}
【问题讨论】:
-
BasePresenter类中的view变量是如何定义的?你可以发布代码吗?
protected var view: V? = null是这样的吗?
标签: android kotlin kotlin-android-extensions