com.airbnb.epoxy.IllegalEpoxyUsage:您必须在添加模型之前在模型上设置 id

问题描述

第一次使用环氧树脂库。出现波纹管错误

rocess: in.droom,PID: 25269
com.airbnb.epoxy.IllegalEpoxyUsage: You must set an id on a model before adding it. Use the @AutoModel annotation if you want an id to be automatically generated for you.

这是控制器代码:

   class MyMatchesController : EpoxyController() {
    override fun buildModels() {
        for (i in 0 until 10) {
            fillBestMatchesNotification {
                id(i)
                bestMatchesCount("100")
            }
        }
    }
}

这是型号代码

    @EpoxyModelClass(layout = R.layout.fill_best_match_notification_layout)
abstract class FillBestMatchesFormNotificationModel : EpoxyModelWithHolder<FillBestMatchesFormNotificationModel.ViewHolder>() {

    @EpoxyAttribute
    var id: Long = 0

    @EpoxyAttribute
    var bestMatchesCount = ""


    override fun getDefaultLayout() = R.layout.fill_best_matches_notification_layout

    @CallSuper
    override fun bind(holder: ViewHolder) {
        super.bind(holder)
        holder.countTV.text = bestMatchesCount
    }

    override fun createNewHolder(): ViewHolder {
        return ViewHolder()
    }

    inner class ViewHolder : EpoxyHolder() {
        lateinit var countTV: TextView
        override fun bindView(itemView: View) {
            countTV = itemView.findViewById(R.id.matchedCountNotificationTV)
        }

    }
}

我尝试从 Model 类中删除 id 但仍然出现相同的错误。

解决方法

id 是保留属性,不得使用 @EpoxyAttribute 声明为新字段。

只需定义所需的属性。 id setter(具有不同的重载)将自动生成。

所以删除以下几行。一切都会好起来的。

@EpoxyAttribute
var id: Long = 0

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...