android - 电子邮件显示为空
问题描述
我有下面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
执行,并且在 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()
}
})
}
}
推荐阅读
- laravel - laravel 集体回报指数值
- java - 无法从 Intellij Idea 部署战争文件构建
- activiti - Activiti script-task中如何调用外部URL或SAP接口?
- javascript - JavaScript 和 dc-js 中的数据表
- dependency-injection - 了解干净架构中的“依赖注入框架是细节”
- android - 在 EditText 中处理“Enter”并在其中插入字符
- django - 将数据从 django 视图传递到模板
- elasticsearch - 如何让 elasticsearch 多匹配模糊搜索始终返回最少数量的结果
- python - How to set a semi-positive Parameter(matrix) in CVXPY?
- javascript - jQuery 如何使 $(this) 选择器触发多个类