'Type mismatch: inferred type is <no name provided> but Callback<Post!>! was expected
I have the function
val client = NetworkService()
val call = client.getService().getAllPost()
call.enqueue(object : Callback<ArrayList<Post>>{
override fun onFailure(call: Call<ArrayList<Post>>, t: Throwable) {
Toast.makeText(this@MainActivity, "Get post failed", Toast.LENGTH_LONG).show()
}
override fun onResponse(
call: Call<ArrayList<Post>>,
response: Response<ArrayList<Post>>) {
response.body()?.let{
post ->
adapter?.updateData(post)
}?: kotlin.run {
Toast.makeText(this@MainActivity, "Get post failed", Toast.LENGTH_LONG).show()
}
}
})
}
*and have the error *
Type mismatch: inferred type is <no name provided> but Callback<Post!>! was expected
in call.enqueue(object : Callback<ArrayList<Post>>
Callback ArrayList dont wanna work with object, idk why
Pls help this it
Solution 1:[1]
It's possible that you've imported a package with the same name as the one you actually meant to import.
Source: This just happened to me. Sometimes I don't read the package names closely enough when IntelliJ auto-imports new classes.
Solution 2:[2]
You probably have somewhere in your code:
fun call(@Body body: Any): Call<Post>
The value to return is not Call<Post> as described in your error:
Type mismatch: inferred type is but Callback<Post!>! was expected
It is Call<List<Post>>:
fun call(@Body body: Any): Call<List<Post>>
(Note that I have put Any as the type of your argument because I don’t know its type.
Solution 3:[3]
Check whether import package is from android or androidx
Solution 4:[4]
make sure that the import is
import retrofit2.Callback
and notimport javax.security.auth.callback.Callback
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | IAmRasputin |
| Solution 2 | Geoffrey R. |
| Solution 3 | Rayan |
| Solution 4 | Anthony Zeren |
