如何将回调函数从Java传递到Kotlin类中。

huangapple go评论101阅读模式
英文:

How to pass callback function from Java to Kotlin class

问题

以下是您要求的翻译内容:

我有一个 Kotlin 类,如下所示(这只是一个示例,为了方便起见,因此它并没有执行任何有用的操作)

  1. class MyKotlinClass(private val handleSomeCallBack: (ByteArray) -> Unit) {
  2. private val someBuffer = BytesBuilder()
  3. fun myFunction(bytesList: List<Byte>) {
  4. handleSomeCallBack(someBuffer.toArray())
  5. }
  6. }

我想从 Java 类中调用此代码,因此在那个类中,我声明了以下内容:

  1. public MyJavaClass() {
  2. messageParser = new MyKotlinClass(this::handleSomeCallback);
  3. }

传递的回调方法是:

  1. private void handleSomeCallback(byte[] dataBytes) {
  2. }

(MyKotlinClass 在文件中被正确声明)

我遇到的问题是,我无法弄清楚如何将回调传递给 MyKotlinClass 的构造函数。

我尝试了各种方法,包括:

  1. messageParser = new MyKotlinClass(handleSomeCallback(dataBytes));
  1. messageParser = new MyKotlinClass(this::handleSomeCallback(dataBytes));

但无论我尝试什么,都会收到错误。

我认为答案与 lambda 表达式有关,但我无法完全理解从 Java 中调用这个的语法应该是什么。

英文:

I have a Kotlin class as follows (this is a sample for ease, hence it doesn't 'do' anything useful)

  1. class MyKotlinClass (private val handleSomeCallBack: (ByteArray) -&gt; Unit) {
  2. private val someBuffer = BytesBuilder()
  3. fun myFunction(bytesList: List&lt;Byte&gt;) {
  4. handleSomeCallBack(someBuffer.toArray())
  5. }
  6. }

I want to call this code from a java class, hence, in that class I have the following declared:

  1. public MyJavaClass() {
  2. messageParser = new MyClass(handleSomeCallback);
  3. }

The callback method being passed is:

  1. private void handleSomeCallback(byte[] dataBytes) {
  2. }

(MyClass is correctly declared within the file)

The issue I'm having is that I can't figure out how to pass the callback to the constructor of MyKotlinClass.

I have tried a variety of things including

messageParser = new MyClass(handleSomeCallback(byte[] dataBytes));

messageParser = new MyClass(this::handleSomeCallback(byte[] dataBytes));

But no matter what I try I receive an error.

I believe the answer is to do with lambdas but I can't quite see what the syntax should be for calling this from Java.

答案1

得分: 1

你可以选择类似这样的方式:

  1. MyKotlinClass instance = new MyKotlinClass(byteArray -> {
  2. // 你的代码
  3. return Unit.INSTANCE;
  4. });

或者像@ADM在这里建议的那样,将Unit用作你单独方法的返回类型:

只需在你的应用程序build.gradle文件的android块中添加以下内容:

  1. compileOptions {
  2. sourceCompatibility JavaVersion.VERSION_1_8
  3. targetCompatibility JavaVersion.VERSION_1_8
  4. }
英文:

You can go with something like this:

  1. MyKotlinClass instance = new MyKotlinClass(byteArray -&gt; {
  2. // your code
  3. return Unit.INSTANCE;
  4. });

Or use Unit as return type of your separate method like suggested by @ADM here

You just need to add

  1. compileOptions {
  2. sourceCompatibility JavaVersion.VERSION_1_8
  3. targetCompatibility JavaVersion.VERSION_1_8
  4. }

to your android block in app build.gradle

huangapple
  • 本文由 发表于 2020年9月17日 23:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63941120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定