我有以下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
答案 0 :(得分:3)
获取输入,单击按钮,否则执行onactivitycreated
并且edittext中没有输入,并为您提供null
。
当用户按下按钮时,从edittext输入,并在edittext中输入,如下所示
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()
}
})
}
}
答案 1 :(得分:1)
Kotlin没有懒惰的评价&#39; :-)所以你应该在reset.setOnClickListener
内获得电子邮件值,或者在执行onActivityCreated
时获取值,并且在执行的其余部分不会修改,包括单击按钮。