我和here和here一样,但是显示错误
这是我的类(class),它的primary constructor
没有secondary constructor
没有init
块。
class RowSubTShirtViewModel(private val subTShirtAdapter: SubTShirtAdapter, val context: TShirtActivity,
val tShirtBean: CommonItemBean, private val parentPosition: Int, private val position: Int) : BaseObservable(), TShirtActivity.setSelectionSubRow {}
创建像
val vm=RowSubTShirtViewModel()
这样的对象给错误
- Create abstract function 'RowSubTShirtViewModel'
- Create function 'RowSubTShirtViewModel'
- Create secondary constructor
- No value passed for parameter
最佳答案
您需要将参数显式传递给主构造函数:
//init params
val subTShirtAdapter = ...
val context = ...
val tShirtBean = ...
val parentPosition = ...
val position = ...
//init viewModel
val vm = RowSubTShirtViewModel(subTShirtAdapter, context, tShirtBean, parentPosition, position)
除非您为参数分配默认值,例如:
class RowSubTShirtViewModel(private val subTShirtAdapter = YourAdapter(),...)
在您的情况下,您可以执行以下操作:
class RowSubTShirtViewModel(val context: TShirtActivity,
private val subTShirtAdapter: SubTShirtAdapter,
val tShirtBean: CommonItemBean,
private val parentPosition: Int = 0,
private val position: Int = 0) : BaseObservable(), TShirtActivity.setSelectionSubRow {}
// and then create an instance
val vm = RowSubTShirtViewModel(yourContext, yourAdapter, yourShirtBean)
关于android - 构造函数中的默认值显示Kotlin中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53499202/