"Not enough information to infer parameter T" with Kotlin and Android
You must be using API level 26 (or above). This version has changed the signature of View.findViewById()
- see here https://developer.android.com/about/versions/oreo/android-8.0-changes#fvbi-signature
So in your case, where the result of findViewById
is ambiguous, you need to supply the type:
1/ Change
val listView = findViewById(R.id.list) as ListView
to
val listView = findViewById<ListView>(R.id.list)
2/ Change
this.label = row?.findViewById(R.id.label) as TextView
to
this.label = row?.findViewById<TextView>(R.id.label) as TextView
Note that in 2/ the cast is only required because row
is nullable. If label
was nullable too, or if you made row
not nullable, it wouldn't be required.
Andoid O change findViewById api from
public View findViewById(int id);
to
public final T findViewById(int id)
so, if you are target to API 26, you can change
val listView = findViewById(R.id.list) as ListView
to
val listView = findViewById(R.id.list)
or
val listView: ListView = findViewById(R.id.list)