Go from one activity to another

-1

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.

    
asked by DavidReyes 30.05.2018 в 17:21
source

1 answer

0

If you do not expect a result of the second activity is badly written, the correct code would be:

Intent intent = new Intent (getApplicationContext (), Profile.class); startActivity (intent);

I see you're using startActivityForResult (); if you do not expect any result of the activity, the problem may be there, I have used it to return the result of a contact or when you select an image from the gallery.

The official information in: link

You have to hang up the Logcat so it's easier.

Greetings and I hope it helps you.

    
answered by 30.05.2018 в 18:40