“Not enough information to infer parameter T” with Kotlin and Android

后端 未结 5 1926
遥遥无期
遥遥无期 2020-12-13 05:17

I\'m trying to replicate the following ListView in my Android app using Kotlin: https://github.com/bidrohi/KotlinListView.

Unfortunately I\'m getting an error I\'m u

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 05:44

    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(R.id.list)

    2/ Change

    this.label = row?.findViewById(R.id.label) as TextView to

    this.label = row?.findViewById(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.

提交回复
热议问题