Kotlin中尚未解析的功能“直到”

时间:2019-01-01 02:02:33

标签: android kotlin android-gradle

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

 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;
        }

3 个答案:

答案 0 :(得分:1)

@Niyati Mehta的回答是正确的。该问题是由于项目vs IDE kotlin插件版本不匹配引起的,但是我想我应该添加一点:

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

一旦找到它,我就通过以下操作查找并更新了IDE版本以匹配项目: Tools-> Kotlin-> Configure Kotlin Plugin Updates-> Check for updates nowSource

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我当时在最新版本的Kotlin中遇到了这个问题:1.3.61

我将代码降级为1.3.60,问题消失了。发行版可能不像应该的那样稳定!我仍然将IDE保持在1.3.61,它在我的build.gradle文件中向我警告说它们不是同一版本,但是警告已经消失了。希望Google尽快在下一版本中解决该问题。