首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能使用提供的参数调用以下函数: Kotlin

不能使用提供的参数调用以下函数: Kotlin
EN

Stack Overflow用户
提问于 2019-06-14 17:45:35
回答 2查看 1.6K关注 0票数 1

我是kotlin的新手,我在构造函数中添加了一个参数,它抛出了这个错误?如何解决我不理解的问题。任何帮助都是非常感谢的。

代码语言:javascript
运行
复制
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())
}

}

EN

回答 2

Stack Overflow用户

发布于 2019-06-14 18:08:31

必须在第一个构造函数中为mList添加一个默认值,或者在第二个构造函数中添加一个StoreViewMap参数

票数 0
EN

Stack Overflow用户

发布于 2019-06-14 18:45:06

你通过调用this(context)来调用你自己的构造函数,这意味着如果你定义了你的构造函数参数,那么调用构造函数就会忽略它们。

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
constructor(context: Context, mlist: StoreViewMap, _listener: OnFragmentInteractionListener, _position: Int) : this(context){
    // your code
}

此构造函数将调用第一个构造函数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56595589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档