且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

“没有足够的信息来推断参数 T"使用 Kotlin 和 Android

更新时间:2023-09-16 08:03:58

您必须使用 API 级别 26(或更高).此版本更改了 View.findViewById() 的签名 - 请参阅此处 https://developer.android.com/about/versions/oreo/android-8.0-changes#fvbi-signature

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

因此,在您的情况下,findViewById 的结果不明确,您需要提供类型:

So in your case, where the result of findViewById is ambiguous, you need to supply the type:

1/改变

val listView = findViewById(R.id.list) as ListView to

val listView = findViewById(R.id.list)

2/改变

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

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

请注意,在 2/中只需要强制转换,因为 row 可以为空.如果 label也可以为空,或者如果您使 row 不可为空,则不需要.

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.