这个错误是因为findContainingViewHolder()方法返回的是RecyclerView.ViewHolder?类型,可能为空,需要进行判空处理。修改代码如下:
```
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
super.onLayoutChildren(recycler, state)
var totalHeight = 0
for (i in 0 until childCount) {
val child = mRvChat.findContainingViewHolder(getChildAt(i))?.itemView
if (child != null) {
totalHeight += child.height
}
}
if (totalHeight > height) {
mRvChat.postDelayed({ mRvChat.smoothScrollToPosition(itemCount - 1) }, 1000)
}
}
```
这样就可以避免Type mismatch: inferred type is View? but View was expected的问题了。