【发布时间】:2018-10-12 22:24:13
【问题描述】:
当我在 Java 中转换此方法时:
private void enqueueDownloads() {
final List<Request> requests = Data.getFetchRequestWithGroupId(GROUP_ID);
fetch.enqueue(requests, updatedRequests -> {
}, error -> Timber.d("DownloadListActivity Error: %1$s", error.toString()));
}
导致这个方法有很多错误:
private fun enqueueDownloads() {
val requests = Data.getFetchRequestWithGroupId(GROUP_ID)
fetch.enqueue(requests, { updatedRequests ->
}, { error -> Timber.d("DownloadListActivity Error: %1\$s", error.toString()) })
}
Kotlin 中的这个方法在 fetch.enqueue 方法中有很多错误,其中值 updatedRequests 和 error 表示 Cannot infer a type for this parameter。
所以我将鼠标悬停在方法上并点击Ctrl+B,库中的方法声明是:
fun enqueue(requests: List<Request>, func: Func<List<Request>>? = null, func2: Func<Error>? = null): Fetch
/** Pause a queued or downloading download.
* @param ids ids of downloads to be paused.
* @param func Callback the paused downloads will be returned on. Note. Only downloads that
* were paused will be returned in the result list.
* @param func2 Callback that is called when attempting to pause downloads fail. An error is returned.
* @throws FetchException if this instance of Fetch has been closed.
* @return Instance
* */
问题与基于方法文档的回调有关,但我无法让它工作!我怎样才能使它完全 Kotlin 并在 Kotlin 中调用它?。
库是Fetch2,用 Kotlin 编写。我也看不到库中方法的完整代码。
【问题讨论】:
-
Func是 kotlin 接口吗?您需要 kotlin 中又长又丑的object : Func<List<String>> { override fun xyz(list: List<String>) { .. }}语法来实现 kotlin 接口。漂亮的短 lambda 语法仅适用于 java 接口。 kotlinlang.org/docs/reference/java-interop.html#sam-conversions “Kotlin 有正确的函数类型,将函数自动转换为 Kotlin 接口的实现是不必要的,因此不受支持。” :) -
您需要创建一个minimal reproducible example。您没有指定库,也没有在问题中添加必要的类/接口,因此很难在没有猜测的情况下回答。
-
好的,现在我已经添加了库名称和链接@Zoe,但我无法访问所有源代码。
-
接口是Java接口,所以不需要长版(直接使用lambda即可)。您可以尝试强制参数。
-
原来你的评论是救命稻草。非常感谢@zapl,我会给你一张去天堂的票。去然后返回。
标签: java android android-studio kotlin fetch2