Reputation: 385
I have a location listener object, and I wanted to know how to define a TextView outside of the activity class. In LayoutInflater.from(context).inflate(R.layout.main, null)
I get an unresolved reference for 'context' and 'main'. For
val locTextView = MainActivity.findViewById(R.id.locTextView) as TextView1
I get an unresolved reference for 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) {}
}
Upvotes: 1
Views: 821
Reputation: 3088
change your this line
val v = LayoutInflater.from(context).inflate(R.layout.main, null)
with
val v = LayoutInflater.from(applicationContext).inflate(R.layout.main, null)
and again change your
MainActivity.findViewById(R.id.locTextView) as TextView
with
val locTextView = findViewById< TextView>(R.id.locTextView)
UPDATE
If you would like to use Kotlin Android Extention , then say goodbye to findViewById()
Upvotes: 2