Kotlin 및 Android에서 "매개 변수 T를 추론하기에 정보가 충분하지 않음"
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! 명시 적으로 지정하십시오.
도움을 주시면 감사하겠습니다.
You must be using API level 26 (or above). This version has changed the signature of View.findViewById()
- see here https://developer.android.com/preview/api-overview.html#fvbi-signature
So in your case, where the result of findViewById
is ambiguous, you need to supply the type:
1/ Change
val listView = findViewById(R.id.list) as ListView
to
val listView = findViewById<ListView>(R.id.list)
2/ Change
this.label = row?.findViewById(R.id.label) as TextView
to
this.label = row?.findViewById<TextView>(R.id.label) as TextView
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.
Andoid O change findViewById api from
public View findViewById(int id);
to
public final T findViewById(int id)
so, if you are target to API 26, you can change
val listView = findViewById(R.id.list) as ListView
to
val listView = findViewById(R.id.list)
or
val listView: ListView = findViewById(R.id.list)
Its Working
API Level 25 or below use this
var et_user_name = findViewById(R.id.et_user_name) as EditText
API Level 26 or Above use this
val et_user_name: EditText = findViewById(R.id.et_user_name)
Happy Coding !
Change your code to this. Where the main changes occurred are marked with asterisks.
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 }**
}
}
I would suggest you to use synthetics
kotlin Android extension:
https://kotlinlang.org/docs/tutorials/android-plugin.html
https://antonioleiva.com/kotlin-android-extensions/
In your case the code will be something like this:
init {
this.label = row.label
}
As simple as that ;)
'IT story' 카테고리의 다른 글
벡터에서 요소 지우기 (0) | 2020.08.31 |
---|---|
모두가 계속 말하는이 '람다'는 무엇입니까? (0) | 2020.08.31 |
2 차원 std :: vector 초기화 (0) | 2020.08.31 |
Appcompatv7-v21 탐색 창에 햄버거 아이콘이 표시되지 않음 (0) | 2020.08.31 |
IE6에서 하나의 (CSS) 차원을 사용하여 이미지 크기를 조정할 때 종횡비를 유지하는 방법은 무엇입니까? (0) | 2020.08.31 |