java - 代码跳转到注册应用中创建账号的错误声明坚持
问题描述
我试图在我的 android 应用程序中创建一个注册活动,当我编译它运行时没有错误,当我运行我的应用程序创建一个新帐户时,它似乎存在逻辑问题,它会跳转到错误代码,因为我已经提供了我的if 语句中的方法 creatnewAccount() 中的注册活动代码,我试图检查 rpassword 和密码是否相同。每当我运行我的应用程序并输入详细信息并单击注册按钮时,它都会跳转到 else 语句。无法理解错误是什么,我该怎么办
package com.nanb.chaton;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import static android.widget.Toast.LENGTH_SHORT;
public class Register extends AppCompatActivity {
private EditText Email, Password, Repassword;
private Button Signup, Login;
private FirebaseAuth mAuth;
private ProgressDialog lodingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
rimplemantaion();
sendusertoLogin();
createAccount();
}
private void createAccount() {
Signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createnewAccount();
}
});
}
private void createnewAccount() {
String email = Email.getText().toString();
String password = Password.getText().toString();
String rpassword = Repassword.getText().toString();
if (TextUtils.isEmpty(email)){
Toast.makeText(this, "Please enter email i'd ", LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this, "Please enter Password ", LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(rpassword)){
Toast.makeText(this, "Please enter Re-Password ", LENGTH_SHORT).show();
}
if (rpassword == password ){
lodingBar.setTitle("Creating new account");
lodingBar.setMessage("Pleased wait till we creat your account");
lodingBar.setCanceledOnTouchOutside(true);
lodingBar.show();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
sendusertoLogin();
Toast.makeText(Register.this,"Account created succcessfully", Toast.LENGTH_SHORT).show();
lodingBar.dismiss();
}else{
Toast.makeText(Register.this,"Sorry,There is a problem while creating your accout. Please try again later", LENGTH_SHORT).show();
lodingBar.dismiss();
}
}
});
}
else{
Toast.makeText(this, "Password and Re-enter password are not same ", LENGTH_SHORT).show();
}
}
private void sendusertoLogin() {
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loginIntent = new Intent(Register.this, Login.class);
startActivity(loginIntent);
}
});
}
private void rimplemantaion() {
Email = (EditText) findViewById(R.id.email);
Password = (EditText) findViewById(R.id.password);
Repassword = (EditText) findViewById(R.id.Rpassword);
Signup = (Button) findViewById(R.id.SignUp);
Login = (Button) findViewById(R.id.LogIn);
lodingBar = new ProgressDialog(this);
}
}
解决方案
替换这个
if (rpassword == password ){
}
else{
Toast.makeText(this, "Password and Re-enter password are not same ", LENGTH_SHORT).show();
}
有了这个
if (rpassword.equals(password) ){
}
else{
Toast.makeText(this, "Password and Re-enter password are not same ", LENGTH_SHORT).show();
}
推荐阅读
- python - 安装 tensorflow 1.x 以使用 tensorflow.contrib
- javascript - 如何以下面给出的格式获取当前日期和时间
- java - 可靠地停止无响应的线程
- docker - 允许在不同桥接网络上的两个 Docker 容器之间的特定端口上进行通信
- c++ - recursive_invoke_result_t 模板结构
- javascript - 如何在本机反应中处理异步函数响应并保存在useState中?
- python - 即使列存在 Keyerror
- gitlab-ci - Gitlab 作业仅在创建合并请求时运行一次
- json - 我想在 Flutter 中将 JSON 数据转换为泛型类型
- java - 如何从java中的数组中返回所有浮点值?