我是 kotlin 的新手,我在构造函数中添加了一个参数,它抛出了这个错误? 如何找出我不明白的问题。任何帮助都是值得赞赏的
Error public constructor AppView(context: Context, _listener: OnFragmentInteractionListener, _position: Int)defined in com.views.home.AppView @JvmOverloads public constructor AppView(mlist: StoreViewMap, context: Context, attrs: AttributeSet? = ..., defStyle: Int = ...) defined in com.views.home.AppView
class AppView @JvmOverloads constructor(mlist: StoreViewMap, context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) :
LinearLayout(context, attrs, defStyle) {
private lateinit var listener: OnFragmentInteractionListener
private var position = 0
private val mainView: View
var mlistener: StoreViewMap = mlist
constructor(context: Context, _listener: OnFragmentInteractionListener, _position: Int) : this(context) {
listener = _listener
position = _position
initFeed()
}
init {
val layoutInflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
mainView = layoutInflater.inflate(R.layout.view_home_feed, this)
}
private fun initFeed() {
mainView.homeSwipeLayout.setOnRefreshListener { fetchSlots() }
loadContentSlots(DataCaching(context).getContentSlots())
}
}
您必须在
第一个构造函数中添加默认值以mList
或在第二个构造函数中添加StoreViewMap
参数
你通过调用this(context(来调用自己的构造函数,这意味着如果你定义构造函数参数,调用构造函数将忽略它们。
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs)
在这里,第一个构造函数调用第二个构造函数,第二个构造函数调用第三个构造函数,但第三个构造函数调用类中继承类LinearLayout
构造函数。
解决方案是创建一个第四构造函数并向其添加您想要的参数,例如:
constructor(context: Context, mlist: StoreViewMap, _listener: OnFragmentInteractionListener, _position: Int) : this(context){
// your code
}
此构造函数将调用第一个构造函数