简体   繁体   English

Kotlin调用kotlin时无法访问kotlin.884033330366588.functions.Function1 function与java lambda

[英]Kotlin cannot access kotlin.jvm.functions.Function1 when calling kotlin function with java lambda

I am trying to call the following Kotlin function from Java我正在尝试从 Java 呼叫以下 Kotlin function

override fun First(list: LinqList<ElementType>, condition: (ElementType) -> Boolean) : ElementType

like this像这样

int first = list.First(list,(x) -> x == 5);

but i get the following error但我收到以下错误

Error java: cannot access kotlin.jvm.functions.Function1
  class file for kotlin.jvm.functions.Function1 not found

I have tried googling it but i can not find the answer anywhere我试过谷歌搜索但我无法在任何地方找到答案

Thanks in advance提前致谢

My problem got fixed when I configured Kotlin compiler and runtime for my Java module with the latest stable version (currently 1.3.30)当我使用最新的稳定版本(当前为 1.3.30)为我的 Java 模块配置 Kotlin 编译器和运行时时,我的问题得到了解决

Just go to Tools > Kotlin > Configure Kotlin in Project > Android with Gradle and choose your Java module with Single module radio button selected then select your version and OK .只需转到Tools > Kotlin > Configure Kotlin in Project > Android with Gradle并选择您的 Java 模块并选择Single module单选按钮,然后选择您的版本和OK

Another Solution:另一个解决方案:

If you have several modules in your android project, please make sure you have added the below config to every module that uses the kotlin:如果您的 android 项目中有多个模块,请确保已将以下配置添加到每个使用 kotlin 的模块中:

Step(1)- Project build.gradle:步骤(1)- 项目 build.gradle:

// Project build.gradle file.
buildscript {
    ext.kotlin_version = '1.3.30'
    ...
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

Step(2)- Inside each module using kotlin:步骤(2)- 在每个模块中使用 kotlin:

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

...

dependencies {
   implementation "androidx.core:core-ktx:1.0.1"
   implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Reference: Add Kotlin to an existing app参考:将 Kotlin 添加到现有应用程序

Android Studio: Android 工作室:

in my case, I have change the signature of the inner function which is making the cause of this exception.就我而言,我更改了内部 function 的签名,这是导致此异常的原因。 try to clean your project and run the app, exception will disappear尝试clean你的项目并运行应用程序,异常将消失

Method 1) Search for Function1 in your project file and rename it to First.方法 1) 在您的项目文件中搜索 Function1 并将其重命名为 First。

Method 2) Search for Function1 in your project file and remove all its occurrences.方法 2) 在您的项目文件中搜索 Function1 并删除它的所有匹配项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM