我一直试图在Android Studio中创建此登录/注册应用程序,但是出现问题。当我转到注册活动时,我无法通过单击我为其设置OnClickListener的TextView来返回登录活动。另外,当我按下“注册活动”上的“注册”按钮时,应该带我回到“登录/主要活动”,但没有。下面,我有两个代码以及清单的代码。有人可以告诉我我的错误在哪里吗?
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cedricpaige.mercurylogin">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MainActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
<activity android:name=".RegActivity"></activity>
</application>
</manifest>
主要活动
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
private TextView userReg;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText) findViewById(R.id.nameText);
Password = (EditText) findViewById(R.id.passText);
Info = (TextView) findViewById(R.id.textView);
userReg = (TextView) findViewById(R.id.tvReg);
Login = (Button) findViewById(R.id.loginButton);
Info.setText("No. of attempts remaining: 5");
firebaseAuth = FirebaseAuth.getInstance();
progressDialog = new ProgressDialog(this);
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
finish();
startActivity(new Intent(MainActivity.this, RegActivity.class));
}
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validate(Name.getText().toString(), Password.getText().toString());
}
});
userReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, RegActivity.class));
}
});
}
private void validate(String userName, String userPassword) {
progressDialog.setMessage("Signing In!");
progressDialog.show();
firebaseAuth.signInWithEmailAndPassword(userName, userPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Login Successful!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}else {
Toast.makeText(MainActivity.this, "Login Failed!", Toast.LENGTH_SHORT).show();
counter--;
Info.setText("Number of attempts remaining: " + counter);
progressDialog.dismiss();
if (counter == 0) {
Login.setEnabled(false);
}
}
}
});
}
}
注册活动
public class RegActivity extends AppCompatActivity {
private EditText userName, userPassword, userEmail;
private Button regButton;
private TextView userLogin;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (validate()) {
String user_email = userEmail.getText().toString().trim();
String pass_word = userPassword.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(user_email,pass_word).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(RegActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegActivity.this, MainActivity.class));
}else {
Toast.makeText(RegActivity.this, "Registration Failed!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
userLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(RegActivity.this, MainActivity.class));
}
});
}
private void setupUIViews() {
userName = (EditText)findViewById(R.id.etUserName);
userPassword = (EditText)findViewById(R.id.etPassword);
userEmail = (EditText)findViewById(R.id.etEmail);
regButton = (Button)findViewById(R.id.buttonSignUp);
userLogin = (TextView)findViewById(R.id.textviewAlready);
}
private Boolean validate() {
Boolean result = false;
String name = userName.getText().toString();
String password = userPassword.getText().toString();
String email = userEmail.getText().toString();
if (name.isEmpty() || password.isEmpty() || email.isEmpty()) {
Toast.makeText(this,"Please enter all details", Toast.LENGTH_SHORT).show();
}else {
result = true;
}
return result;
}
答案 0 :(得分:0)
在您的MainActivity中,您的onCreate()中有这行代码
if (user != null) {
finish();
startActivity(new Intent(MainActivity.this, RegActivity.class));
}
这就像authListener()一样工作,当您的Activity启动时,它将查看用户是否为!= null来输入该块。
当您在RegisterActivity中使用此代码时
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (validate()) {
String user_email = userEmail.getText().toString().trim();
String pass_word = userPassword.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(user_email,pass_word).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(RegActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegActivity.this, MainActivity.class));
}else {
Toast.makeText(RegActivity.this, "Registration Failed!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
if (task.isSuccessful()) {...
之后,您将重新启动MainActivity,它将在onCreate()
内部再次检查用户是否为!= null
。
由于您刚刚注册了用户,因此自动getCurrentUser() != null
,因此它将在声明中输入并完成MainActivity.class
,然后重新启动RegisterActivity.class
一个简单的解决方案不是将用户带回登录活动(MainActivity.class),而是在用户登录后将您的用户发送到Activity,因为现在用户是!= null,因此您无需请求再次登录用户。
如果这不是您的用例,只需从finish()
中删除MainActivity
即可完成工作,而不是对您的RegisterActivity
进行其他操作,只需在完成之后task.isSuccefull
您可以在MainActivity中的if语句上放置调试断点,以查看用户的更改,然后可以确保用户为null或不为空
提示
我看到您为变量使用大写名称,而不是这样做
private Button Login;
执行此操作
private Button mBtnLogin;
它不会更改您的代码,但是使用CamelCase
定义变量将减少将来您的应用程序开发将其与某些方法调用混淆的风险