如何解决Kotlin(no lambda)中的重载解析歧义?
我在这一行中有Overload Resolution Ambiguity错误:
departureHourChoice!!.selectionModel.select(currentHourIndex)
以供参考:
-
departureHourChoice
是一个来自java.scene.control
的ChoiceBox<Int>
-
currentHourIndex
是一个Int
-
重载解析歧义发生在
.select()
方法中。 它被重载并可以接受两种参数:(T obj)
或(int index)
。 -
.select()
方法允许选择一个ChoiceBox
的项目,并且可以通过引用该项目或其索引来确定可以选择哪个项目。 在这种情况下,我希望它被索引(int
)选中。 -
这是错误的照片
如何解决过载分辨率歧义?
看来你被这个错误所击中,你可以这样做:
-
框中
currentHourIndex
:lateinit var departureHourChoice: ChoiceBox<Int> ... val currentHourIndex = 1 departureHourChoice.selectionModel.select(currentHourIndex as Int?)
-
或者改变
ChoiceBox
声明来使用java.lang.Integer
而不是Kotlin的Int
:lateinit var departureHourChoice: ChoiceBox<java.lang.Integer> ... val currentHourIndex = 1 departureHourChoice.selectionModel.select(currentHourIndex)
进一步阅读:
- 为什么Java方法的Integer参数映射到Int而不是平台类型?
- Kotlin:如果Java库有原始类型和盒装类型的重载,我该怎么办?
尝试转换为Int
:
departureHourChoice!!.selectionModel.select(currentHourIndex as Int)