除了我之前关于上述问题的问题,我还在这里放置了完整的代码。正如我在之前的问题中提到的,我已经在 Fun() 之前将 Auth 初始化为 uAuth=FirebaseAuth.getInstance() 并包含私有 var uAuth: FirebaseAuth? = null 在类 RegisterAcitivity 下的代码开头。但问题仍然存在。我是否将 uAuth 的初始化放在了错误的位置?请帮忙。谢谢
package com.example.shoponlinenicheteck
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.material.textfield.TextInputLayout
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.FirebaseDatabase
import org.w3c.dom.Text
class RegisterActivity : AppCompatActivity() {
var ufirstname: EditText? = null
var ulastname: EditText? = null
var uemail: EditText? = null
var upassword: EditText? = null
var uconfpassword: EditText? = null
var ucontactno: EditText? = null
var btnRegister: Button? = null
var userFirstNameWrapper: TextInputLayout? = null
var userLastNameWrapper: TextInputLayout? = null
var userEmailWrapper: TextInputLayout? = null
var userPasswordWrapper: TextInputLayout? = null
var userConfPasswordWrapper: TextInputLayout? = null
var userContactNoWrapper: TextInputLayout? = null
private var uAuth: FirebaseAuth? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
val ufirstname = findViewById<View>(R.id.userFirstName)
val ulastname = findViewById<View>(R.id.userLastName)
val uemail = findViewById<View>(R.id.userEmailAddress)
val upassword = findViewById<View>(R.id.userPassword)
val uconfpassword = findViewById<View>(R.id.userConfPassword)
val ucontactno = findViewById<View>(R.id.userContactNumber)
val userFirstNameWrapper = findViewById<TextInputLayout>(R.id.userFirstNameWrapper)
val userLastNameWrapper = findViewById<TextInputLayout>(R.id.userlastNameWrapper)
val userEmailWrapper = findViewById<TextInputLayout>(R.id.userEmailWrapper)
val userPasswordWrapper = findViewById<TextInputLayout>(R.id.userPasswordWrapper)
val userConfPasswordWrapper = findViewById<TextInputLayout>(R.id.userConfPasswordWrapper)
val userContactNoWrapper = findViewById<TextInputLayout>(R.id.userContactNoWrapper)
val btnRegister=findViewById<Button>(R.id.btnRegister)
}
init {
var onClick: Unit
run {
if (uAuth!!.currentUser != null) {
//User is Logged in and can redirect to another activity.
val firstname = ufirstname!!.text.toString().trim { it <= ' ' }
val lastname = ulastname!!.text.toString().trim { it <= ' ' }
val email = uemail!!.text.toString().trim { it <= ' ' }
val password = upassword!!.text.toString().trim { it <= ' ' }
val confpassword = uconfpassword!!.text.toString().trim { it <= ' ' }
val contactno = ucontactno!!.text.toString().trim { it <= ' ' }
if (firstname.isEmpty()) {
userFirstNameWrapper!!.error = "Enter First Name"
userFirstNameWrapper!!.requestFocus()
}
if (lastname.isEmpty()) {
userLastNameWrapper!!.error = "Enter Last Name"
userLastNameWrapper!!.requestFocus()
}
if (email.isEmpty()) {
userEmailWrapper!!.error = "Enter Email"
userEmailWrapper!!.requestFocus()
}
if (password.isEmpty()) {
userPasswordWrapper!!.error = "Enter Password"
userPasswordWrapper!!.requestFocus()
}
if (confpassword.isEmpty()) {
userConfPasswordWrapper!!.error = "Enter Confirm Password"
userConfPasswordWrapper!!.requestFocus()
}
if (password != confpassword) {
userConfPasswordWrapper!!.error = "Password didn't match"
userConfPasswordWrapper!!.requestFocus()
}
if (contactno.isEmpty()) {
userContactNoWrapper!!.error = "Enter Contact No"
userContactNoWrapper!!.requestFocus()
}
uAuth=FirebaseAuth.getInstance()
fun () {
onStart()
super.onStart()
//Check if user is signed in (non-null) and update UI accordingly
val user = User(firstname, lastname, email, contactno)
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().currentUser!!.uid)
var onComplete: Unit
}
uAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(OnCompleteListener { AuthResult})
var onComplete: Unit
var task: NonNull; Task<AuthResult?>
(
if (task.isSuccessful()) {
val user = User(firstname, lastname, email, contactno)
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().currentUser!!.uid)
.setValue(user)
.addOnCompleteListener(OnCompleteListener<Unit>())
var onComplete: Unit
NonNull
var task: Task<Void?>
(
if (task.IsSuccessful()) Toast.makeText(
this@RegisterActivity,
"User created successfully.",
Toast.LENGTH_LONG.show()
) else
Toast.makeText(
this@RegisterActivity,
task.getException().getMessage(),
Toast.LENGTH_LONG.show()
)
)
} else Toast.makeText(
this@RegisterActivity,
task.getException().getMessage(),
Toast.LENGTH_LONG.show()
)
)
}
}
}
}