다음 Java 코드를 Kotlin으로 변환하려고합니다. 그것을 컴파일하고 잘 작동합니다. 내가 현재 가지고있는 코드에서 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;
}
}
, 나는
Required: Nothing, Found: MvpViewHolder
을 내용의
presenter.bindView(this)
에 오류가 발생합니다.
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
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 클래스에서 어떻게 정의됩니까? 코드를 게시 할 수 있습니까? 'protected var view : V? = null'이게 이런가요? –