使用Kotlin和Android,“信息不足以推断参数T”


110

我正在尝试使用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)类型推断失败:没有足够的信息来有趣地推断参数T findViewById(p0:Int):T!请明确指定。

我将不胜感激。


2
您可以尝试更改this.label = ... as TextViewthis.label = row?.findViewById<TextView>并这样做val listView = ...吗?请让我知道这是否可行,以便在这种情况下可以做出正确的回答。
ChristianBrüggemann17年

1
哪条线会导致错误?
voddan '17

您可以举一个较小的例子来说明问题吗?
voddan '17

@ChristianBrüggemann像这样:i.imgur.com/ZeWKjt5.png和这个:i.imgur.com/Can7w0p.png?有了您的编辑,现在有这些错误:i.imgur.com/qqPAjrL.png
蒂莫昆腾

1
试试这个this.label = row?.findViewById <TextView>(R.id.label)作为TextView
Alf Moh

Answers:


224

您必须使用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不可为空,则不需要。


10
这是批量解决此问题的方法:打开“ 在路径中替换”对话框(Ctrl + Shift + R)并选中“正则表达式”框。替换findViewById\((.+?)\)\s+as\s+(.+)findViewById<$2>\($1\)并在所有文件上运行替换。它解决了我几乎所有的错误。
古斯塔夫·卡尔森

1
为什么在Java中默认情况下足以推断View类型而在Kotlin中却不足?findViewById(R.id.tabLayout).setOnClickListener(v-> Log.d(TAG, "login: "));对于Java,这是可以的。
uTha9

findViewById\((.+?)\)\s+as\s+([A-Za-z0-9?]+)对我来说效果更好。它可以防止某些行代码未通过@GustavKarlsson
user3680200

1
链接没有这么说。
Alston

7

Andoid O从以下位置更改find​​ViewById api

公共视图findViewById(int id);

公共最终T findViewById(int id)

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

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

val listView = findViewById(R.id.list)

要么

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


4

它的工作

API级别25或以下使用此

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

API Level 26以上

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

编码愉快!


2

将代码更改为此。主要更改发生的位置标有星号。

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,则以下答案是正确的答案。val listView = findViewById<ListView>(R.id.list)
EricWasTaken

@ericWasTaken是的,您是对的。编译器应该从方括号中的内容推断类型。有时它失败了。您的代码也是正确的。将更新我的答案以反映更改。
阿尔夫·莫

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.