Issue
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
TextView
is the name of the class. You need to apply setText on an instance of the class. just like you did
editTextTextPersonName.text.toString()
instead of
EditText.text.toString()
I don't know that your TextView is called but you then need to do
instanceOfYourTextView.setText("entered value: ${input}")
Answered By - Ivo Beckers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.