为什么在 MainActivity.kt 的 setOnCLickListener 中调用 AppCompatEditText 文本时未被访问?

Why is AppCompatEditText text not being accessed when called in setOnCLickListener in MainActivity.kt?

我指的是用于创建测验应用程序的 YouTube 视频。我正在完全复制,但我的代码仍然无法正常工作。

activity_main.xml 中,我将 AppCompatEditText 放在 TextInputLayout 中。在 MainActivity.kt 中,我尝试访问 EditText 中的文本,但似乎无论我做什么文本仍然是 null,尽管我们可以在屏幕上的文本框中看到它。

MainActivity.kt :

package com.example.quizapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.quizapp.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    lateinit var binding : ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN

        btn_start.setOnClickListener {
            if (et_name.text.toString().isEmpty()) {
                Toast.makeText(this@MainActivity, "Please enter a name", Toast.LENGTH_SHORT).show()
            } else {
                val intent = Intent(this@MainActivity, QuizQuestionsActivity::class.java)
                startActivity(intent)
                finish()
            }
        }
    }
}

我试图将 if 语句更改为 if(et_name?.text.toString().isEmpty()),但这会使代码跳到 else 语句。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@drawable/ic_bg"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:gravity="center"
        android:text="@string/quizapp"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:textStyle="bold" />

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:background="@color/white"
        app:cardCornerRadius="8dp"
        app:cardElevation="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/welcome"
                android:textColor="@color/dark_grey"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/please_enter_your_name"
                android:textColor="@color/light_grey"
                android:textSize="16sp" />

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/til_name"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/et_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="actionGo"
                    android:hint="@string/name"
                    android:inputType="textCapWords"
                    android:textColorHint="@color/light_grey"
                    android:textColor="@color/dark_grey" />

            </com.google.android.material.textfield.TextInputLayout>

            <Button
                android:id="@+id/btn_start"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="@color/purple_500"
                android:text="@string/start"
                android:textColor="@color/white"
                android:textSize="18sp">

            </Button>
        </LinearLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>

YouTube 视频:Android masterclass: building a quiz app. His drive link: His code for the app

GitHub 回购到我的应用程序:QuizApp

binding.btn_start.setOnClickListener{
            if(binding.et_name.text.isBlank()) {
                Toast.makeText(this, "Please enter a name", Toast.LENGTH_SHORT).show()
            } else {
                val intent = Intent(this, QuizQuestionsActivity::class.java)
                startActivity(intent)
                finish()
            }
        }

顺便说一下,你应该使用 viewBinding,kotlin 扩展已被弃用,它可能会导致 NullPoint 异常