I am brand new to Kotlin as well as Android. I’ve searched everywhere but could not find the solution to this. I am trying to modify a text field in the ‘class MainActivity : AppCompatActivity()’ from a running thread ‘class datarun: Runnable, MainActivity()’. But the program always crashes without any suggestions. I think this has to do with the fact that I am trying to access something from a different class, but I am not sure how to pass that information to the thread. Any suggestions would be appreciated.
open class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val threadWithRunnable = Thread(datarun())
threadWithRunnable.start()
}}
class datarun: Runnable, MainActivity() {
override fun run() {
while (true) {
findViewById(R.id.textView).apply {
text = “First”}
sleep(1000L)
findViewById(R.id.textView).apply {
text = “Second”}
sleep(1000L)
} }}