如何解决Kotlin中的过载分辨率歧义(无lambda)?

时间:2016-08-05 01:10:12

标签: java javafx kotlin

我在此行中遇到Overload Resolution Ambiguity错误:

departureHourChoice!!.selectionModel.select(currentHourIndex)

供参考:

  • departureHourChoiceChoiceBox<Int>,来自java.scene.control

  • currentHourIndexInt

  • 过载分辨率歧义发生在.select()方法中;它已重载,可以接受两种参数:(T obj)(int index)

  • .select()方法允许选择ChoiceBox中的项目,您可以通过引用该项目或其索引来确定可以选择哪个项目。在这种情况下,我希望它由Index(int)选择。

  • 以下是错误enter image description here

  • 的照片

如何解决过载分辨率歧义?

3 个答案:

答案 0 :(得分:9)

您可能会遇到this bug作为解决方法:

  • 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)
    

进一步阅读:

答案 1 :(得分:0)

尝试转换为Int

departureHourChoice!!.selectionModel.select(currentHourIndex as Int)

答案 2 :(得分:0)

在类似情况下针对我的解决方案是在您的导入中定义例如: 将kotlin.math.sqrt导入为kotsqrt

然后用作: val a = kotsqrt(2.3)

相关问题