I have just started using kotlin and I have a block of code in java which I have to convert to kotlin. This is the java code:
public class NonSwipeableViewPager extends ViewPager
{
public NonSwipeableViewPager(Context context) {
super(context);
setMyScroller();
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setMyScroller();
}
private void setMyScroller() {
//some code
}
}
If there was only one constructor in this code, I could have written like this:
class NonSwipeableViewPager(context: Context): ViewPager(context) {
init {
setMyScroller()
}
private fun setMyScroller() {
//some code
}
}
But, as there are two constructors and each constructor calls super()
method, I couldn't figure out how can I convert this code to kotlin. The closest I have achieved is this:
class NonSwipeableViewPager(context: Context): ViewPager(context) {
init {
setMyScroller()
}
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
setMyScroller()
}
private fun setMyScroller() {
//some code
}
}
But, in this code, i am getting the following error in this line super(context!!, attrs)
:
primary constructor call expected
So, how can I call super()
from the secondary constructor?