Kotlin 编译错误:Unresolved reference: mutableListOf

文章目录

    下面一段 Kotlin 代码在编译时报错:

    class ResultListAdapter(val resultList: mutableListOf<PercentPrice>):
            RecyclerView.Adapter<ResultListAdapter.ResultViewHolder>() {
    

    报错信息为:

    Unresolved reference: mutableListOf
    

    原来,mutableListOf 是一个函数方法,只能在类型 MutableList 初始化时使用。

    所以,这里应该使用类 MutableList。

    class ResultListAdapter(val resultList: MutableList<PercentPrice>):
            RecyclerView.Adapter<ResultListAdapter.ResultViewHolder>() {
    

    mutableListOf 的使用范例

    val prices = mutableListOf<PercentPrice>()