电子邮件显示为空



我有以下XML密码重置表单,用户在其中输入电子邮件并单击提交:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.LoginFragment">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment" />
<EditText                               // This is read as null!
android:id="@+id/myEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset" />
</LinearLayout>
</FrameLayout>

在片段文件中,我有:

class ResetPasswordFragment : Fragment() {
companion object {
fun newInstance() = ResetPasswordFragment()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.fragment_reset_password, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val auth = FirebaseAuth.getInstance()
val email = myEmail.text.toString().trim()   // This is null!
reset.setOnClickListener {
Toast.makeText(context, "Email entered is: $email", Toast.LENGTH_SHORT).show()
auth.sendPasswordResetEmail(email)
.addOnCompleteListener({ task ->
if (task.isSuccessful) {
Toast.makeText(context, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Failed to send reset email!: ${task.exception}", Toast.LENGTH_SHORT).show()
}
})
}
}
}

val email = myEmail.text.toString().trim()中,它给出了null,而val email = myEmail.text显示正确的输入为Editable,但这不被auth.sendPasswordResetEmail(email)接受,因为输入需要String

获取输入,当单击按钮时,否则onactivitycreated执行,并且编辑文本中没有输入并为您提供null

当用户按下按钮时,从编辑文本中获取输入,并且输入在编辑文本中可用,如下所示

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val auth = FirebaseAuth.getInstance()

reset.setOnClickListener {
Toast.makeText(context, "Email entered is: $email", Toast.LENGTH_SHORT).show()
val email = myEmail.text.toString().trim()
//^^^^^^^^^^^^^^^^^^^^^^^^^
auth.sendPasswordResetEmail(email)
.addOnCompleteListener({ task ->
if (task.isSuccessful) {
Toast.makeText(context, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Failed to send reset email!: ${task.exception}", Toast.LENGTH_SHORT).show()
}
})
}
}

Kotlin 没有"懒惰评估":-(因此,您应该在reset.setOnClickListener中获取电子邮件值,否则该值将在执行时onActivityCreated获取,并且在执行的其余部分(包括单击按钮(不会修改。

最新更新