'kotlin-android-extensions not working. what will be the problem?
I was following some guide(download android studio today) of kotlin and I have use the setText and it's not working. what will be the problem?
package com.example.basic
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
Toast.makeText(applicationContext, "button was pressed.", Toast.LENGTH_LONG).show()
}
button2.setOnClickListener {
val input = editTextTextPersonName.text.toString()
TextView.setText("entered value: ${input}")
}
}
}
(I had tried replace setText to text but it's still red and can't save it)
Unresolved reference: setText(error)
Solution 1:[1]
As Mayur Gajra did mention you are not using the view from the XML but instead you are using the TextView class and this is your problem, what you need to have instead is something like this:
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
And then your MainActivity should look like the following:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
Toast.makeText(applicationContext, "button was pressed.", Toast.LENGTH_LONG).show()
}
button2.setOnClickListener {
val input = editTextTextPersonName.text.toString()
text.setText("entered value: ${input}")
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pako1 |
