这里我正在进行登录活动,它使用Firebase存储用户的用户名和密码,当我尝试在模拟器上运行此活动时,其显示应用已停止。我检查了两次代码,但找不到该错误。我授予Internet权限以及Firebase客户端,身份验证和Core库。是否需要其他任何库
package com.example.firebasedemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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 com.google.firebase.auth.FirebaseUser;
public class LoginActivity extends AppCompatActivity {
EditText user1,pass2;
Button button;
TextView textView;
FirebaseAuth mFirebaseauth;
private FirebaseAuth.AuthStateListener mAutoSateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
user1=(EditText) findViewById(R.id.username);
pass2=(EditText)findViewById(R.id.password);
button=(Button)findViewById(R.id.signup);
textView=(TextView)findViewById(R.id.toSignIN);
mFirebaseauth=FirebaseAuth.getInstance();
mAutoSateListener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mFirebaseUser=mFirebaseauth.getCurrentUser();
if (mFirebaseUser!=null)
{
Toast.makeText(LoginActivity.this,"You are logged in",Toast.LENGTH_SHORT).show();
Intent i=new Intent(LoginActivity.this,HomeActivity.class);
startActivity(i);
}
else {
Toast.makeText(LoginActivity.this,"Please Login",Toast.LENGTH_SHORT).show();
}
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username=user1.getText().toString();
String password=pass2.getText().toString();
if (username.isEmpty())
{
user1.setError("Please enter email ID");
user1.requestFocus();
}
else if (password.isEmpty())
{
pass2.setError("Please enter Password");
pass2.requestFocus();
}
else if (username.isEmpty()&& password.isEmpty())
{
Toast.makeText(LoginActivity.this,"Fields are Empty",Toast.LENGTH_SHORT).show();
}
else if (!username.isEmpty()&& password.isEmpty())
{
mFirebaseauth.signInWithEmailAndPassword(username,password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(LoginActivity.this,"Login error ,Please login Again",Toast.LENGTH_SHORT).show();
}
else {
Intent i=new Intent(LoginActivity.this,HomeActivity.class);
startActivity(i);
}
}
});
}
else {
Intent intent=new Intent(LoginActivity.this,HomeActivity.class);
startActivity(intent);
}
}
});
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i =new Intent(LoginActivity.this,MainActivity.class);
startActivity(i);
}
});
}
protected void onStart()
{
super.onStart();
mFirebaseauth.addAuthStateListener(mAutoSateListener);
}
}
答案 0 :(得分:1)
我的猜测是您在此行的逻辑上有一些错误吗?
else if (!username.isEmpty()&& password.isEmpty())
我认为应该是
else if (!username.isEmpty()&& !password.isEmpty())
此外,在此处显示错误消息也很有帮助。