“没有足够的信息来推断参数T".与Kotlin和Android [英] "Not enough information to infer parameter T" with Kotlin and Android

查看:419
本文介绍了“没有足够的信息来推断参数T".与Kotlin和Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Kotlin在Android应用中复制以下ListView: https://github.com/bidrohi/KotlinListView .

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 unable to resolve myself. Here's my code:

MainActivity.kt:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val listView = findViewById(R.id.list) as ListView
    listView.adapter = ListExampleAdapter(this)
}

private class ListExampleAdapter(context: Context) : BaseAdapter() {
    internal var sList = arrayOf("Eins", "Zwei", "Drei")
    private  val mInflator: LayoutInflater

    init {
        this.mInflator = LayoutInflater.from(context)
    }

    override fun getCount(): Int {
        return sList.size
    }

    override fun getItem(position: Int): Any {
        return sList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
        val view: View?
        val vh: ListRowHolder

        if(convertView == null) {
            view = this.mInflator.inflate(R.layout.list_row, parent, false)
            vh = ListRowHolder(view)
            view.tag = vh
        } else {
            view = convertView
            vh = view.tag as ListRowHolder
        }

        vh.label.text = sList[position]
        return view
    }
}

private class ListRowHolder(row: View?) {
    public val label: TextView

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

布局与此处完全相同: https://github.com/bidrohi/KotlinListView/tree/master/app/src/main/res/layout

The layouts are exactly as here: https://github.com/bidrohi/KotlinListView/tree/master/app/src/main/res/layout

我得到的完整错误消息是: 错误:(92,31)类型推断失败:没有足够的信息来有趣地推断参数T findViewById(p0:Int):T! 请明确指定.

The full error message I'm getting is this: Error:(92, 31) Type inference failed: Not enough information to infer parameter T in fun findViewById(p0: Int): T! Please specify it explicitly.

我将很高兴能获得任何帮助.

I'd appreciate any help I can get.

推荐答案

您必须使用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

val listView = findViewById<ListView>(R.id.list)

2/更改

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

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

这篇关于“没有足够的信息来推断参数T".与Kotlin和Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆