gpt4 book ai didi

android - Caused by : java. lang.AbstractMethodError: Kotlin中的抽象方法

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:02 25 4
gpt4 key购买 nike

我这里有这段代码:

interface MyClass {
fun main() {
File(TentDatabase.getPathRepository(context)).walkBottomUp()
.filter { it.isFile }
.forEach { parseFiles(it) } //I think here is the problem
}

fun parseFiles(file: File) {
val extension = file.path.substringAfterLast('.', "")
Log.i("DB", "name - $extension")
}
}

当我尝试运行此代码时出现异常:

Caused by: java.lang.AbstractMethodError: abstract method "void org.secfirst.umbrella.data.internal.TentDao.parseFiles(java.io.File)"

如何将函数作为参数传递?我只想为每次迭代执行 parseFiles(file)。

最佳答案

针对您的问题:

How do I pass a function as a parameter? I just want to execute parseFiles(file) for each iteration

您可以简单地替换:

.forEach { parseFiles(it) }

与:

.forEach(::parseFiles)

它代表一个引用,您正在传递函数的名称,该函数可以处理 forEach 将生成的参数类型。


但是你得到的异常与其他东西有关。我认为这与您最终字节码的版本有关。我在使用

的环境中测试了您的代码
sourceCompatibility JavaVersion.VERSION_1_8

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}

因此在 Java 8 中,您可以访问接口(interface)上的 default 方法,Kotlin 默认具有此方法,但如果您的目标是 Java 7,则可能无法访问此方法。这是我的猜测。

关于android - Caused by : java. lang.AbstractMethodError: Kotlin中的抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50608461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com