Android Studio Kotlin variables

Viewed 34

ok, brand new to android studio and kotlin (not programming in general)

installed Android Studio, set up a couple of hardware profiles (and enabled dev mode). Here is my issue:

Simple app, 'every time you click the button, the number above doubles.'

in the first_fragment.xml file, I rename the ID to 'textDisplayedValue' in the MainActivity.kt file, I create 2 variables originalValue and newValue.

When I try to set the originalValue to the value in textDisplayedValue, I get an 'Unresolved reference' error

Code

Log

In addition, when I launch the debugger, the 'ok' button is disabled. I assume that is due to the code errors so correct me if I am wrong.

Thanks in advance for the help.

Layout File

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".FirstFragment">

    <TextView
        android:id="@+id/textDisplayedValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintBottom_toTopOf="@id/button_first"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textDisplayedValue" />
</androidx.constraintlayout.widget.ConstraintLayout>

       binding.fab.setOnClickListener { view ->
            val originalValue = 1
            val newValue = originalValue * 2
            textDisplayedValue = newValue.toString()

            Snackbar.make(view, "Value $originalValue changed to $newValue", Snackbar.LENGTH_LONG).show()
        }
1 Answers

Try this

  1. Comment the not working code
  2. From the toolbar select A. Build -> clean project B. After that Build -> rebuild project
  3. Uncomment the not working code and do this
val originalValue = binding.textDisplayedValue. text() .toString().toLong()
val newValue = originalValue * 2
Related