当我尝试创建新帐户时,创建帐户按钮不会在android studio中触发而不会进入下一个活动,即使屏幕上没有显示错误消息。请帮我解决这个问题。
这是Kotlin版本以及当用户没有填写下面提到的任何字段时如何显示错误消息。
class RegisterActivity : AppCompatActivity( ) {
var mAuth: FirebaseAuth? = null
var mDatabase: DatabaseReference? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
mAuth = FirebaseAuth.getInstance()
createaccountET.setOnClickListener {
val name = accountdisplaynameET.text.toString().trim()
val email = registeremailET.text.toString().trim()
val password = registerpasswordET.text.toString().trim()
val reenterpassword = reenterpasswordET.text.toString().trim()
val Mobile = mobilenumberET.text.toString().trim()
val date = dateofbirthET.text.toString().trim()
val gender = genderET.text.toString().trim()
if (!TextUtils.isEmpty(name) && name.length > 3 ||
!TextUtils.isEmpty(email) && !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() ||
!TextUtils.isEmpty(Mobile) && Mobile.length != 10 ||
!TextUtils.isEmpty(password) && password.length < 4 && password.length > 10 ||
!TextUtils.isEmpty(reenterpassword) && reenterpassword.length < 4 && reenterpassword.length > 10 && reenterpassword != password ||
!TextUtils.isEmpty(date) ||
!TextUtils.isEmpty(gender)) {
createAccount(email, password,name,Mobile, date,gender )
} else {
Toast.makeText(this, "Please fill out the fields", Toast.LENGTH_LONG)
.show()
}
}
}
fun createAccount(email: String, password: String, name: String, Mobile: String, date: String, gender: String) {
mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener {
task: Task<AuthResult> ->
if (task.isSuccessful) {
val currentUser = mAuth!!.currentUser
val userId = currentUser!!.uid
mDatabase = FirebaseDatabase.getInstance().reference
.child("Users").child(userId)
/*
Users Profile Information
*/
val userObject = HashMap<String, String>()
userObject.put("display_name", name)
userObject.put("Mobile", Mobile)
userObject.put("date", date)
userObject.put("email", email)
userObject.put("gender", gender)
userObject.put("image", "default")
userObject.put("thumb_image", "default")
mDatabase!!.setValue(userObject).addOnCompleteListener {
task: Task<Void> ->
if (task.isSuccessful) {
Toast.makeText(this, "User Created", Toast.LENGTH_LONG)
.show()
val purchaseIntent = Intent(this, PurchaseActivity::class.java)
startActivity(purchaseIntent)
finish()
} else {
Toast.makeText(this@RegisterActivity, "User Not Created!", Toast.LENGTH_LONG)
.show()
}
}
}
}
}
}