我在Kotlin中实现了一个RecylcerView.Adapter类。 我收到编译时错误,请参阅以下代码中的注释。
// Compile time Error: 'public' function exposes its 'internal' return type ViewHolder
class DietListAdapter(context: Context, private val foodList: ArrayList<Food>) : RecyclerView.Adapter<DietListAdapter.ViewHolder>() {
private val inflater: LayoutInflater
private var onItemClick: Callback<Void, Int>? = null
init {
inflater = LayoutInflater.from(context)
}
// Compile time Error: 'public' function exposes its 'internal' return type ViewHolder
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DietListAdapter.ViewHolder {
val holder = ViewHolder(inflater.inflate(R.layout.layout_food_list_item, parent, false))
return holder
}
// Compile time Error: 'public' function exposes its 'internal' parameter type ViewHolder
override fun onBindViewHolder(holder: DietListAdapter.ViewHolder, position: Int) {
holder.textViewFoodName.text = foodList[position].foodName
holder.textViewFoodDesc.text = foodList[position].foodDesc
holder.itemView.setOnClickListener {
if (onItemClick != null)
onItemClick!!.callback(foodList[position].foodId)
}
}
...
...
internal inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textViewFoodName: TextView
var textViewFoodDesc: TextView
init {
textViewFoodName = itemView.findViewById(R.id.textViewFoodName) as TextView
textViewFoodDesc = itemView.findViewById(R.id.textViewFoodDesc) as TextView
}
}
...
...
}
我已经在Kotlin文档中检查了它,没有解决方案。
还有其他人遇到过这个问题吗?
答案 0 :(得分:34)
我的坏,一个愚蠢的错误。我在Android Studio中将Java代码转换为Kotlin,因此它将内部类转换为内部内部类。
我刚删除内部工作正常。
我打算删除这个问题,只是想到有人可能会遇到同样的问题,所以只是发布了一个答案。
答案 1 :(得分:4)
这个问题有两个方面:
如果您将外部类视为公共类,那么Kotlin不允许您将其私有或内部成员公开为超类参数,或者作为返回类型甚至作为参数公开。内部类应该与eclosing类具有相同的修饰符,即public。
另一个方面是,如果您将外部类作为私有或内部,那么在这种情况下,您的内部类可以分别是私有或内部。
答案 2 :(得分:1)
在我的情况下,一条消息是:&#34;&#39; public&#39;功能暴露其内部&#39;参数类型SomeType&#34;。在此之前,我已将类可见性从internal
更改为:
class ClassName(type: SomeType, ...)
所以,我找到了SomeType类:
internal enum class SomeType
并删除internal
。