[英]Passing lambda function from java class to kotlin class gives me error
My kotlin class:我的科特林课:
class InstallationReceiver(activity : RootActivity, url : String, progressCallback :
((percentage : Int) -> Unit)?, callback : ((isUpdated : Boolean) -> Unit)?, packageName:
String){
//my code
}
I am passing lambda fun to this class from java class like this:我将 lambda fun 从 java 类传递给这个类,如下所示:
new InstallationReceiver(getActivity(), apkUrl, integer -> **{
String text = "Installing app"+integer;
textView.setText(text);
}**,true,null);
Why this lambda function is demanding a Unit return type?.为什么这个 lambda 函数需要一个 Unit 返回类型?。 I even tried returning Unit.INSTANCE but nothing helped me out.我什至尝试返回 Unit.INSTANCE 但没有任何帮助。 It's still showing error.它仍然显示错误。
What are those asterisks?那些星号是什么?
I tried this minimal example and it works:我试过这个最小的例子,它的工作原理:
Kotlin科特林
class InstallationReceiver(
a: ((percentage: Int) -> Unit)?
)
Java爪哇
new InstallationReceiver(percentage -> {
String text = "Installing app" + percentage;
return Unit.INSTANCE;
});
Also, you're passing true
and null
where the constructor expects a lambda ( (isUpdated : Boolean) -> Unit
) and a non-nullable String
.此外,您正在传递true
和null
,其中构造函数需要一个 lambda ( (isUpdated : Boolean) -> Unit
)和一个不可为空的String
。
You can get away with compiling while passing a null string (you shouldn't since the app will crash at runtime), but for sure you need to pass a lambda instead of a boolean.您可以在传递空字符串的同时进行编译(您不应该这样做,因为应用程序会在运行时崩溃),但可以肯定的是,您需要传递 lambda 而不是布尔值。
Something similar to the example above:类似于上面的例子:
isUpdated -> {
// your code
return Unit.INSTANCE;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.