代码之家  ›  专栏  ›  技术社区  ›  Michael

为什么我在尝试按ID访问视图时会得到null?

  •  0
  • Michael  · 技术社区  · 2 年前

    我有一个函数cauntMe(),单击按钮时会触发该函数。

    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()
    }
    

    以下是视图的XML声明:

    <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" />
    

    在触发函数中,此行返回null:

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

    以下是课程:

    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()
        }
    }
    

    <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>
    

    为什么textView视图为空?

    1 回复  |  直到 2 年前
        1
  •  1
  •   AIMIN PAN    2 年前

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

    这将从活动的内容根中查找,并应得到它。