我是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())
}
}
发布于 2019-06-14 18:08:31
必须在第一个构造函数中为mList
添加一个默认值,或者在第二个构造函数中添加一个StoreViewMap
参数
发布于 2019-06-14 18:45:06
你通过调用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
的构造函数。
解决方案是创建第四个构造函数,并在其中添加您想要的参数ex:
constructor(context: Context, mlist: StoreViewMap, _listener: OnFragmentInteractionListener, _position: Int) : this(context){
// your code
}
此构造函数将调用第一个构造函数
https://stackoverflow.com/questions/56595589
复制相似问题