Не могу обратиться к элементам activity_main
Хочу изменить текст в textView через MainActivity.kt, но мой код не видит элемент. Пробовал с кнопками также, но не помогло.
Прилагаю вам MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun toastMe(view: View) {
val myToast = Toast.makeText(this, "Toast", Toast.LENGTH_SHORT)
myToast.show()
}
fun countMe(view: View) {
val countString = textView.text.toString()
var count: Int = Integer
count++
textView.text = count.ToString()
}
}
ActivityMain
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/screenBackground"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="151dp"
android:layout_height="40dp"
android:fontFamily="sans-serif-smallcaps"
android:text="@string/hello_worldText"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" />
<Button
android:onClick="toastMe"
android:id="@+id/toast_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:background="@color/bottunBackground"
android:text="toast"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.501" />
<Button
android:id="@+id/count_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/bottunBackground"
android:text="count"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/random_button"
app:layout_constraintStart_toEndOf="@+id/toast_button"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/random_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:background="@color/bottunBackground"
android:text="random"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.501" />
</androidx.constraintlayout.widget.ConstraintLayout>```
Источник: Stack Overflow на русском