【问题标题】:"Unresolved reference: etName" in android studio with kotlin带有 kotlin 的 android studio 中的“未解决的参考:etName”
【发布时间】:2021-11-18 04:09:31
【问题描述】:

我在 Kotlin 中开发一个 Android 项目,但在这段代码中出现错误:

 val btnSignUp: Button = findViewById(R.id.btnSignUp)

        btnSignUp.setOnClickListener{
            val userName = etName.text.toString()
            val email = etEmail.text.toString()
            val password = etPassword.text.toString()
            val confirmPassword = etConfirmPassword.text.toString()

            if(TextUtils.isEmpty(userName)){
                Toast.makeText(applicationContext,"username is required", Toast.LENGTH_SHORT).show()
            ......
<EditText
            android:id="@+id/etName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:background="@color/colorTextBackground"
            android:hint="@string/enter_name"
            android:textColor="@color/colorText" />

        <EditText
            android:id="@+id/etEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"            
            android:inputType="textEmailAddress"
            android:background="@color/colorTextBackground"
            android:hint="@string/enter_email"
            android:textColor="@color/colorText" />

        <EditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"           
            android:inputType="textPassword"
            android:background="@color/colorTextBackground"
            android:hint="@string/enter_password"
            android:textColor="@color/colorText" />

        <EditText
            android:id="@+id/etConfirmPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:background="@color/colorTextBackground"
            android:hint="@string/enter_confirm_password"
            android:textColor="@color/colorText" />

第二部分代码是etnameetPasswordetConfirmPassword在xml文件中定义的。

错误是Unresolved reference: etName`

etPasswordetConfirmPassword 出现同样的错误。 如果需要,我可以发布更多代码。

提前致谢!

【问题讨论】:

  • 欢迎来到stackoverflow。您能否请edit 提出您的问题并将代码设为minimal reproducible example?我们需要能够重现问题以帮助您。
  • 请发布更多代码。向我们展示您定义 etName 和其他代码的部分。
  • 是在 layout.xml 文件中定义的 EditText 的 etName id 吗?如果您使用的是所有工具的最新版本,那么您应该学习视图绑定。 developer.android.com/topic/libraries/view-binding#kotlin
  • 是的,我正在使用 layout.xml 文件中定义的 EditText 的 etName id。

标签: android android-studio kotlin


【解决方案1】:

变量etNameetEmail 和其他变量已在XML 布局 文件中定义,因此您不能在您的Kotlin 代码中使用它们。首先,通过R 文件将它们存储在一个变量中,如下所示:

val etName: EditText = findViewById(R.id.etName)
val etEmail: EditText = findViewById(R.id.etEmail)
val etPassword: EditText = findViewById(R.id.etPassword)
val etConfirmPassword: EditText = findViewById(R.id.etConfirmPassword)

您还可以通过访问 cmets 中提供的链接来使用 View Binding。 现在,您可以毫无问题地使用您的变量了。

【讨论】:

  • 非常感谢。
  • 您可以为答案投票并将其标记为已接受以表达您的感激之情。不客气!
猜你喜欢
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 2023-01-17
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多