我有一个位置监听器对象,我想知道如何在活动类之外定义TextView。在LayoutInflater.from(context).inflate(R.layout.main, null)
我得到了一个未解决的' context'和' main'。对于
val locTextView = MainActivity.findViewById(R.id.locTextView) as TextView1
我得到了一个未解决的findViewById参考。
private val locationListener: LocationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
val v = LayoutInflater.from(context).inflate(R.layout.main, null)
val locTextView = MainActivity.findViewById(R.id.locTextView) as TextView
locTextView.setText("" + location.longitude + ":" + location.latitude);
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
override fun onProviderEnabled(provider: String) {}
override fun onProviderDisabled(provider: String) {}
}
答案 0 :(得分:2)
更改此行
val v = LayoutInflater.from(context).inflate(R.layout.main, null)
与
val v = LayoutInflater.from(applicationContext).inflate(R.layout.main, null)
又一次
改变你的
MainActivity.findViewById(R.id.locTextView) as TextView
带
val locTextView = findViewById< TextView>(R.id.locTextView)
更新
如果您想使用Kotlin Android Extention,请告别findViewById()