类型不匹配。必需:找不到:回调<*>

时间:2019-07-28 12:40:22

标签: android kotlin retrofit2

在我的android项目中:

import retrofit2.Call
import retrofit2.Callback

    fun getAdvertising(callback: Callback<List<Advertising>>) {
        val call = tangoRestClient.advertising
        executeAsync(call, callback)
    }

    private fun executeAsync(call: Call<*>, callback: Callback<*>) {
        call.enqueue(callback)  // compile error
    }

但是我得到了编译错误:

> Task :app:generateDebugSources
> Task :app:validateSigningDebug

> Task :transport:compileDebugKotlin FAILED
e: \TransportService.kt: (127, 14): Out-projected type 'Call<*>' prohibits the use of 'public abstract fun enqueue(p0: Callback<T!>!): Unit defined in retrofit2.Call'

FAILURE: Build failed with an exception.

1 个答案:

答案 0 :(得分:3)

是的,CallCallback的类型参数应该相同。您必须这样编写executeAsync函数:

private fun <T> executeAsync(call: Call<T>, callback: Callback<T>) {
    call.enqueue(callback)  // no compile error
}

这将确保类型参数相同