"Not enough information to infer parameter T" 使用 Kotlin 和 Android

"Not enough information to infer parameter T" with Kotlin and Android

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

很遗憾,我遇到了一个我无法自行解决的错误。 这是我的代码:

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

我收到的完整错误消息是这样的: 错误:(92, 31) 类型推断失败:没有足够的信息来推断 fun findViewById(p0: Int) 中的参数 T:T! 请明确说明。

如果能得到任何帮助,我将不胜感激。

将您的代码更改为此。发生主要变化的地方标有星号。

package com.phenakit.tg.phenakit

import android.content.Context
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView

public class MainActivity : AppCompatActivity() {

    private var mTextMessage: TextView? = null

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                mTextMessage!!.setText(R.string.title_home)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                mTextMessage!!.setText(R.string.title_dashboard)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                setContentView(R.layout.activity_list_view)
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

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

        mTextMessage = findViewById(R.id.message) as TextView?
        val navigation = findViewById(R.id.navigation) as BottomNavigationView
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)


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



        **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 var label: TextView

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

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

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

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 不可为空,则不需要它。

Andoid O 将 findViewById 从

更改为 api

public View findViewById(int id);

public final T findViewById(int id)

所以,如果你的目标是 API 26,你可以更改

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

val listView = findViewById(R.id.list)

val listView: ListView = findViewById(R.id.list)

正常工作

API 25级以下使用此

    var et_user_name = findViewById(R.id.et_user_name) as EditText

API 26级以上使用此

    val et_user_name: EditText = findViewById(R.id.et_user_name)

编码愉快!

我建议你使用 synthetics kotlin Android extension:

https://kotlinlang.org/docs/tutorials/android-plugin.html

https://antonioleiva.com/kotlin-android-extensions/

在你的情况下,代码将是这样的:

init {
   this.label = row.label
}

就这么简单 ;)

In android 12 从代码中删除 as 并将 model in <> 放在 getParcelableExtra.[=14 前面=]

改变

intent.getParcelableExtra("MyModel") as MyModel

intent.getParcelableExtra `<MyModel>` ("MyModel")!!