0

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()
        }
  • I'm going to guess that you want `binding.textDisplayedValue`, but without the layout file and Kotlin source in text (not images), it is difficult to be certain. – CommonsWare Sep 19 '22 at 23:29
  • You are trying to access a fragment view from the activity. https://stackoverflow.com/questions/62406558/android-get-view-of-fragment-in-activity It's hard to help you without the code. – Leonardo Velozo Sep 19 '22 at 23:39
  • ok, I think I have the relevant code displayed – DaedalusK71 Sep 19 '22 at 23:51
  • @CommonsWare When I type binding.textDisplayValue, it throws an error 'Unresolved reference textDisplayedValud' – DaedalusK71 Sep 20 '22 at 00:16
  • if I remove the textDisplayValue variable from the MainActivity.kt file, the app runs. – DaedalusK71 Sep 20 '22 at 00:23
  • Your `binding` has a `fab` in it, but the layout file you posted does not. So evidently, you have two different layout files in play. Each layout file should have its own binding property if you're using view binding. – Tenfour04 Sep 20 '22 at 01:01

1 Answers1

0

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
Ammar Yasser
  • 88
  • 1
  • 8
  • when I type 'binding.' the intellisense shows options but as soon as I type 'text' it displays the 'unresolved reference text' error – DaedalusK71 Sep 20 '22 at 00:24
  • may be you are inflating the wrong layout check your bindings if it reference to the same layout that includes `textDisplayedValue` – Ammar Yasser Sep 20 '22 at 09:02