【问题标题】:Unresolved reference in Kotlin for the function 'until'Kotlin 中未解决的函数“直到”引用
【发布时间】:2019-05-28 06:58:44
【问题描述】:

我正在尝试将 Java 项目转换为 Kotlin。我收到一个非常奇怪的编译时错误,函数 until 是“未解决的引用”,为什么无法识别此函数 until

 fun renderChildrenToRight(canvas: Canvas, startIndex: Int, stopIndex: Int) {
    val itemRight = nodePosition!!.x + nodeRectLimits.right
    val itemExternalPaddingWidth = getRenderAttribute(AttributeExternalPaddingWidth, AttributeDefaultExternalPadding)
    val itemTop = nodePosition!!.y + nodeRectLimits.top
    val itemExternalPaddingHeight = getRenderAttribute(AttributeExternalPaddingHeight, AttributeDefaultExternalPadding)
    val childItemsSize = getChildItemsSize(startIndex, stopIndex)
    var nextItemTop = itemTop + childItemsSize / 2
    val x = itemRight + itemExternalPaddingWidth


    for (i in startIndex until stopIndex) {
        val currentNode = _children[i]
        val bulletDesiredHeight = currentNode.desiredHeightWithChildren
        val y = nextItemTop - bulletDesiredHeight / 2
        currentNode.setNodePosition(x, y)


        currentNode.renderWithChildren(canvas, BulletRenderStyle.ToTheRight)
        nextItemTop -= bulletDesiredHeight + itemExternalPaddingHeight
    }
}

这是我的旧 Java 方法:

 public void renderChildrenToRight(Canvas canvas, int startIndex, int stopIndex) {
        int itemRight = getNodePosition().x + getNodeRectLimits().right;
        int itemExternalPaddingWidth = getRenderAttribute(AttributeExternalPaddingWidth, AttributeDefaultExternalPadding);
        int itemTop = getNodePosition().y + getNodeRectLimits().top;
        int itemExternalPaddingHeight = getRenderAttribute(AttributeExternalPaddingHeight, AttributeDefaultExternalPadding);
        int childItemsSize = getChildItemsSize(startIndex, stopIndex);
        int nextItemTop = itemTop + childItemsSize / 2;
        int x = itemRight + itemExternalPaddingWidth;


        for (int i = startIndex; i < stopIndex; i++) {
            Node currentNode = _children.get(i);
            int bulletDesiredHeight = currentNode.getDesiredHeightWithChildren();
            int y = nextItemTop - bulletDesiredHeight / 2;
            currentNode.setNodePosition(x, y);


            currentNode.renderWithChildren(canvas, BulletRenderStyle.ToTheRight);
            nextItemTop -= bulletDesiredHeight + itemExternalPaddingHeight;
        }

【问题讨论】:

  • until 是 Kotlin 标准库中的函数,而不是关键字。你的类路径中有标准库吗?
  • 其实有一个类似的问题,而且我收到的回复基本上说的是原始回复中的内容。我们应该将其标记为重复吗?我应该接受答案还是不接受?谢谢你让我知道until是一个函数而不是一个关键字,很有趣。问题还是解决了。
  • @Drocchio 如果您愿意,您可以接受答案,它可能会有所帮助。谁偶然发现了你的问题。不过,我会投票决定将其关闭为重复项,因为它使指向具有更多答案的站点的链接更加可见。

标签: android kotlin android-gradle-plugin


【解决方案1】:

您的 Intellij IDEA 插件和您在项目中使用的 Kotlin 运行时/编译器需要匹配。您的 android studio 必须升级为支持 kotlin。检查所有这些并重新启动您的 android studio。

【讨论】:

    【解决方案2】:

    @Niyati Mehta 的回答是正确的。该问题是由项目与 IDE kotlin 插件版本不匹配引起的,但我认为我应该再补充一点:

    我可以让 IDE 确认这一点的唯一方法(您应该在 gradle 文件上收到黄色警告)是重新安装整个 IDE,因为显然将其更新为 3.4 到 3.5 版本搞砸了(我还删除了 .android 和.android-studio 文件夹,不确定是否还需要)。直到那时我才在项目的 gradle 文件上收到警告。

    得到它后,我查找并更新了 IDE 版本以匹配项目: Tools -> Kotlin -> Configure Kotlin Plugin Updates -> Check for updates now (Source)

    希望这也可以帮助其他人。

    【讨论】:

      【解决方案3】:

      当时我在使用最新版本的 Kotlin 时特别遇到了这个问题:1.3.61

      我将代码降级到 1.3.60,问题就消失了。可能版本没有应有的稳定!我仍然将我的 IDE 保持在 1.3.61,它在我的 build.gradle 文件中警告我它们不是相同的版本,但警告已经消失。希望 Google 很快会在下一个版本中解决此问题。

      【讨论】:

        【解决方案4】:

        文件 -> 无效现金/重新启动 -> 无效并重新启动

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多