【问题标题】:Kotlin parameter type mismatchKotlin 参数类型不匹配
【发布时间】: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


【解决方案1】:

您的bindView 方法需要View 作为参数。

当您定义一个变量(在 BasePresenter 上定义的view)时会出现您看到的错误,该变量可以保存空结果,或者在您的情况下是一个 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&lt;*,*&gt; 有关,这意味着在这种情况下 BasePresenter&lt;Nothing, Nothing&gt;(在 kotlin 中没有对象) - 在 this 链接上阅读更多关于 kotlin 中的 星形投影 的信息。

我建议明确定义基本演示者期望的类型或明确定义为BasePresenter&lt;Any?, Any?&gt;

【讨论】:

  • 我稍微研究了一下,但意识到这在 Java 端不是问题。那么它是如何与 Java 代码一起工作的呢?
  • @Rafa 我想我知道问题出在哪里。我会更新答案
  • @Rafa 你能测试一下建议的答案吗?
  • 那行得通,它带来了一些关于扩展 BasePresenter 类的类型参数的其他错误,但我认为这些超出了这个问题的范围,我现在可以修复,因为我理解了解决方案.
【解决方案2】:

您不能直接使用它来指向当前类。

你需要使用

this@class_name

例如,如果“Example”是class名称,您可以使用

this@Example

在 Java 中表示this

欲了解更多信息,请访问https://kotlinlang.org/docs/reference/this-expressions.html

【讨论】:

    猜你喜欢
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多