Michael
Michael

Reputation: 13636

Why I get null when I try to access view by ID?

I have a function, cauntMe() that is triggered when the button is clicked.

fun cauntMe(view: View){
    val textView = view.findViewById<TextView>(R.id.textView)
    val countString = textView.text.toString()
    var count: Int = Integer.parseInt(countString)

    count++
    textView.text = count.toString()
}

Here is XML declaration of the view:

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFEB3B"
    android:text="@string/hello_world_text"
    android:textSize="96sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.45"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.27" />

In triggered function this row return null:

 view.findViewById<TextView>(R.id.textView)

Here is class:

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }


    fun cauntMe(view: View){
        val textView = view.findViewById<TextView>(R.id.textView)
        val countString = textView.text.toString()
        var count: Int = Integer.parseInt(countString)

        count++
        textView.text = count.toString()
    }
}

Here is the activity:

<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="#3F51B5"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFEB3B"
        android:text="@string/hello_world_text"
        android:textSize="96sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.45"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.27" />


    <Button
        android:onClick="cauntMe"
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="84dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="84dp"
        android:text="Count"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.04" />

</androidx.constraintlayout.widget.ConstraintLayout>

Why textView view is null?

Upvotes: 0

Views: 36

Answers (1)

AIMIN PAN
AIMIN PAN

Reputation: 1665

note the function fun cauntMe(view: View) carries the view which is the button (indicate which button is clicked), view.findViewById is to look up the child inside that button, which obviously is null.

So simply replace it with

val textView = findViewById<TextView>(R.id.textView)

This looks up from the activity's content root and should get it.

Upvotes: 1

Related Questions