Android 无法使用提供的参数调用以下函数:Kotlin
我是kotlin的新手,我在构造函数中添加了一个参数,它会抛出这个错误吗?我不明白如何解决这个问题。任何帮助都是值得感激的Android 无法使用提供的参数调用以下函数:Kotlin,android,kotlin,Android,Kotlin,我是kotlin的新手,我在构造函数中添加了一个参数,它会抛出这个错误吗?我不明白如何解决这个问题。任何帮助都是值得感激的 Error public constructor AppView(context: Context, _listener: OnFragmentInteractionListener, _position: Int)defined in com.views.home.AppView @JvmOverloads public constructor AppView(mlist
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
参数您必须在第一个构造函数中为mList
添加默认值,或者在第二个构造函数中添加StoreViewMap
参数您正在调用自己的构造函数构造函数,这意味着如果定义构造函数参数,调用构造函数将忽略它们
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
的构造函数
解决方案是创建一个forth构造函数,并向其中添加所需的参数,例如:
constructor(context: Context, mlist: StoreViewMap, _listener: OnFragmentInteractionListener, _position: Int) : this(context){
// your code
}
此构造函数将调用第一个构造函数您通过调用此(上下文)调用自己的构造函数,这意味着如果定义构造函数参数,调用构造函数将忽略它们
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
的构造函数
解决方案是创建一个forth构造函数,并向其中添加所需的参数,例如:
constructor(context: Context, mlist: StoreViewMap, _listener: OnFragmentInteractionListener, _position: Int) : this(context){
// your code
}
此构造函数将调用第一个构造函数您的第一个构造函数有4个参数,2个带有默认值,在第二个构造函数中,您调用的是第一个构造函数->此(上下文),因此未提供参数mList,这会导致错误,您应该调用第一个构造函数->此(mList,上下文)或者在第一个构造函数中提供一个默认值,就像使用attrs和defstyle一样。第一个构造函数有4个参数,2个带有默认值,在第二个构造函数中调用第一个构造函数->this(上下文),因此没有提供参数mList,这会导致错误,您应该调用第一个构造函数->this(mList,context),或者在第一个构造函数中提供一个默认值,就像使用attrs和defStyle一样