&引用;没有足够的信息来推断参数T“;用Kotlin和Android

&引用;没有足够的信息来推断参数T“;用Kotlin和Android,android,kotlin,android-8.0-oreo,Android,Kotlin,Android 8.0 Oreo,我正在尝试使用Kotlin在我的Android应用程序中复制以下ListView: 不幸的是,我遇到了一个我自己无法解决的错误。 这是我的密码: MainActivity.kt: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val listView = find

我正在尝试使用Kotlin在我的Android应用程序中复制以下ListView:

不幸的是,我遇到了一个我自己无法解决的错误。 这是我的密码:

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
    }
}
}
布局如下所示:

我收到的完整错误消息如下: 错误:(92,31)类型推断失败:没有足够的信息来推断fun findViewById中的参数T(p0:Int):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 }**
    }
}
package com.phenakit.tg.phenakit
导入android.content.Context
导入android.os.Bundle
导入android.support.design.widget.BottomNavigationView
导入android.support.v7.app.AppActivity
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget.BaseAdapter
导入android.widget.ListView
导入android.widget.TextView
公共类MainActivity:AppCompatActivity(){
私有变量mTextMessage:TextView?=null
private val mOnNavigationItemSelectedListener=BottomNavigationView.OnNavigationItemSelectedListener{item->
时间(item.itemId){
R.id.navigation\u主页->{
mTextMessage!!.setText(R.string.title\u home)
return@OnNavigationItemSelectedListener真的
}
R.id.navigation\u仪表板->{
mTextMessage!!.setText(R.string.title\u仪表板)
return@OnNavigationItemSelectedListener真的
}
R.id.navigation\u通知->{
setContentView(R.layout.activity\u列表\u视图)
return@OnNavigationItemSelectedListener真的
}
}
假的
}
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mTextMessage=findviewbyd(R.id.message)作为文本视图?
val navigation=findViewById(R.id.navigation)作为BottomNavigationView
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
**val listView=findViewById(R.id.list)**
**listView?.adapter=ListExampleAdapter(此)**
}
私有类ListExampleAdapter(上下文:context):BaseAdapter(){
内部var sList=arrayOf(“EIN”、“Zwei”、“Drei”)
私人val mInflator:LayoutInflater
初始化{
this.mInflator=LayoutInflater.from(上下文)
}
重写fun getCount():Int{
返回列表大小
}
覆盖趣味getItem(位置:Int):任意{
返回滑轨[位置]
}
覆盖getItemId(位置:Int):长{
返回位置。toLong()
}
覆盖视图(位置:Int,convertView:View?,父级:ViewGroup):视图{
val视图:视图?
val vh:ListRowHolder
if(convertView==null){
视图=此.mInflator.inflate(R.layout.list_行,父项,false)
vh=列表行保持器(视图)
view.tag=vh
}否则{
视图=转换视图
vh=view.tag作为ListRowHolder
}
vh.label.text=sList[位置]
返回视图
}
}
私有类ListRowHolder(行:视图?){
公共变量标签:TextView
**init{this.label=row?.findviewbyd(R.id.label)作为TextView}**
}
}

您必须使用API级别26(或更高)。此版本已更改
View.findViewById()的签名
-请参阅此处

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

1/改变

val listView=findViewById(R.id.list)作为listView
to

val listView=findviewbyd(R.id.list)

2/改变

this.label=row?.findViewById(R.id.label)作为文本视图

this.label=row?.findViewById(R.id.label)作为文本视图

请注意,在2/中,之所以需要强制转换,是因为
可为空。如果
标签

也可以为null,或者如果您使
不可为null,则不需要它。

Andoid O change findViewById api from

公共视图findViewById(内部id)

公共最终T findViewById(内部id)

因此,如果您是API 26的目标,您可以更改

val listView=findviewbyd(R.id.list)作为listView

val listView=findViewById(R.id.list)

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


它正在工作

    var et_user_name = findViewById(R.id.et_user_name) as EditText
    val et_user_name: EditText = findViewById(R.id.et_user_name)
API等级25或以下使用此

    var et_user_name = findViewById(R.id.et_user_name) as EditText
    val et_user_name: EditText = findViewById(R.id.et_user_name)
API等级26或以上使用此

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

快乐编码

我建议您使用
synthetics
kotlin Android扩展:

在您的情况下,代码如下所示:

init {
   this.label = row.label
}

就这么简单;)

你能试着改变一下这个标签吗。。。作为TextView
this.label=row?.findViewById
,并对
val listView=…
执行类似操作?请让我知道这是否有效,这样我就可以在这种情况下做出正确的回答。哪一行会导致错误?你能用一个较小的例子来说明这个问题吗?@ChristianBrüggemann这样说:还有这个:?编辑后现在会出现以下错误:尝试将this.label=row?.findviewbyd(R.id.label)作为textview。如果您的应用程序以API 26为目标,则下面的答案是正确的
val listView=findViewById(R.id.list)
@ericwastake是的,你说得对。编译器应该根据括号中的内容推断类型。有时它在这方面失败了。您的代码也是正确的。将更新我的答案以反映更改。以下是如何批量解决此问题:打开替换路径拨号