1
votes

I am new to kotlin and I have added a parameter in the constructor and it throws this error? how to figure out the problem I don't understand. Any help would be appreciatable

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())
}

}

2

2 Answers

0
votes

You have to add a default value to mList in your first constructor or add a StoreViewMapparameter in your second constructor

0
votes

You're calling your own constructor by calling this(context), which mean that if you define your constructor parameters calling the constructor will ignore them.

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)

Here the first constructor is calling the second and the second is calling the third, but the third is calling the constructor of the inherited class LinearLayout in your class.

The solution is to create a forth constructor and adding to it the params you want ex:

constructor(context: Context, mlist: StoreViewMap, _listener: OnFragmentInteractionListener, _position: Int) : this(context){
    // your code
}

This constructor will call the first constructor