I cannot display a Toast in 2 activitys from the same package

Asked

Viewed 30 times

-1

I am doing a registration screen and login in my app, in the registration screen I implemented some exception treatments toasts, at the time of emulating the app the registration screen toasts display normally. I did a similar process for the login screen but it does not display the warnings. I tried to change the package login screen but it still didn’t work. what may be going on ?

Obs. the registration screen is as Main41activity and the login screen is like Main4activity

package com.example.institutofecomercio.TELA4;

public class Main41activity extends Appcompatactivity { private Edittext fieldName, fieldEmail, fieldName; Private Button Boots Private Firebaseuth Tools; Private user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main41);


    campoNome = findViewById(R.id.editNome);
    campoEmail = findViewById(R.id.editEmail);
    campoSenha = findViewById(R.id.editSenha);
    botaoCadastrar = findViewById(R.id.buttonCadastrar);



    botaoCadastrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            String textoNome = campoNome.getText().toString();
            String textoEmail = campoEmail.getText().toString();
            String textoSenha = campoSenha.getText().toString();


            //Validar se os campos foram preenchidos
            if (!textoNome.isEmpty()) {
                if (!textoEmail.isEmpty()) {
                    if (!textoSenha.isEmpty()) {
                        usuario = new Usuario();
                        usuario.setNome(textoNome);
                        usuario.setEmail(textoEmail);
                        usuario.setSenha(textoSenha);

                        cadastrarUsuario();

                    } else {
                        Toast.makeText(Main41Activity.this,
                                "Preencha a senha!",
                                Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(Main41Activity.this,
                            "Preencha o email!",
                            Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(Main41Activity.this,
                        "Preencha a senha!",
                        Toast.LENGTH_SHORT).show();


            }
        }
    });


}
public void cadastrarUsuario() {
    autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
    autenticacao.createUserWithEmailAndPassword(
            usuario.getEmail(), usuario.getSenha()

    ).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {

                Toast.makeText(Main41Activity.this,
                        "sucesso ao cadastrar o usuario!",
                        Toast.LENGTH_SHORT).show();
            } else {
               String execao = "";
                try {
              throw task.getException();
               } catch ( FirebaseAuthWeakPasswordException e) {
                    execao = "Digite uma senha mais consistente!";
                } catch ( FirebaseAuthInvalidCredentialsException e) {
                         execao = "Por favor, digite um e-mail válido";
                } catch (FirebaseAuthUserCollisionException e) {
                   execao = "esta conta já foi cadastrada";
                } catch (Exception e){
                execao = "Erro ao cadastrar usuário" + e.getMessage();
                   e.printStackTrace();
                }
                    Toast.makeText(Main41Activity.this,
                            execao,
                        Toast.LENGTH_SHORT).show();


            }
        }
    });
}}




package com.example.institutofecomercio.TELA4;

public class Main4activity extends Appcompatactivity { private Edittext campoEmail, fieldNew; Private button buttonFind; Private user; Private Firebaseuth Accessories;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    campoEmail = findViewById(R.id.editEmail1);
    campoSenha = findViewById(R.id.editSenha1);
    botaoEntrar = findViewById(R.id.buttonEntrar1);

    botaoEntrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String textoEmail = campoEmail.getText().toString();
            String textoSenha = campoSenha.getText().toString();

            if ( !textoEmail.isEmpty() ){
                if ( !textoSenha.isEmpty() ){

                    usuario = new Usuario();
                    usuario.setEmail( textoEmail );
                    usuario.setSenha( textoSenha );
                    validarLogin();

                }else {
                    Toast.makeText(Main4Activity.this,
                            "Preencha a senha!",
                            Toast.LENGTH_LONG).show();
                }
            }else {
                Toast.makeText(Main4Activity.this,
                        "Preencha o email!",
                        Toast.LENGTH_LONG).show();
            }

        }
    });

}

public void validarLogin() {

    autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
    autenticacao.signInWithEmailAndPassword(
            usuario.getEmail(),
            usuario.getSenha()
    ).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if (task.isSuccessful()) {


                Toast.makeText(Main4Activity.this,
                        "Sucesso ao fazer login",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(Main4Activity.this,
                        "Erro ao fazer login",
                        Toast.LENGTH_LONG).show();


                String excecao = "";
                try {
                    throw task.getException();
                } catch (FirebaseAuthInvalidUserException e) {
                    excecao = "Usuário não está cadastrado.";
                } catch (FirebaseAuthInvalidCredentialsException e) {
                    excecao = "E-mail e senha não correspondem a um usuário cadastrado";
                } catch (Exception e) {
                    excecao = "Erro ao cadastrar usuário: " + e.getMessage();
                    e.printStackTrace();
                }

                Toast.makeText(Main4Activity.this,
                        excecao,
                        Toast.LENGTH_LONG).show();
            }
        }
    });

}}

1 answer

-1

try to use

getApplicationContext()

in place of

Main4Activity.this

Browser other questions tagged

You are not signed in. Login or sign up in order to post.