I get the user data and password from Firebase, but when using the code to pass another activity, the app is closed.
Toast.makeText(Login.this, "Bienvenido", Toast.LENGTH_SHORT).show();
Intent intent = new Intent (Login.this, Perfil.class);
startActivityForResult(intent, 0);
I tried using the previous code but I would like to know if it is written correctly.
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usuario = findViewById(R.id.txtNameUser);
pass = findViewById(R.id.txtPass);
aceptar = findViewById(R.id.btnAceptar);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference dbRef = ref.child("CAPPCD");
final DatabaseReference pacRef = dbRef.child("Paciente");
final Login lgn;
aceptar.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(final View v)
{
user = usuario.getText().toString().trim();
con = pass.getText().toString().trim();
validarDatos(user, con);
for(int i=1; i<=2; i++)
{
pac = "Paciente"+i;
final DatabaseReference numPacRef = pacRef.child(pac);
DatabaseReference nomUserRef = numPacRef.child("nombreDeUsuario");
nomUserRef.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
String nomUs = dataSnapshot.getValue(String.class);
if(nomUs.equals(user))
{
p++;
DatabaseReference passRef = numPacRef.child("contrasena");
passRef.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dS)
{
String passUs = dS.getValue(String.class);
if(passUs.equals(con))
{
p++;
todo2 = pac;
Toast.makeText(Login.this, "Bienvenido", Toast.LENGTH_SHORT).show();
Intent intent = new Intent (getApplicationContext(), Perfil.class);
startActivityForResult(intent, 0);
}
}
@Override
public void onCancelled(DatabaseError dE)
{
Toast.makeText(getApplicationContext(), "Ha fallado la red: "+dE.getCode(), Toast.LENGTH_LONG).show();
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError)
{
Toast.makeText(getApplicationContext(), "Ha fallado la red: "+databaseError.getCode(), Toast.LENGTH_LONG).show();
}
});
}
if(p == 3)
{
Toast.makeText(Login.this, "Bienvenido", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Login.this, Perfil.class));
}
else
{
Toast.makeText(Login.this, "Nombre de usuario o contraseña incorrectos", Toast.LENGTH_SHORT).show();
}
}
});Click(View v) {
Intent registrar = new Intent(Login.this, Registrar.class);
startActivity(registrar);
}
});
}
What happens is that when trying to open the profile, the app is closed.