首页 > 解决方案 > 电子邮件显示为空

问题描述

我有下面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

标签: androidkotlin

解决方案


获取输入,当单击按钮时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()
                        }
                    })
        }
    }

推荐阅读