首页 文章

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

提问于
浏览
10

我在这一行中遇到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 回答

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

    进一步阅读:

  • 0

    尝试转换为 Int

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

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

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

相关问题