Toast is not displayed

Asked

Viewed 633 times

1

I’m having a problem Toast doesn’t show up when I make a request on for my web service, the whole method stays inside my thread, I did a test putting a message out of the thread then it shows up, but right where I need it, it does not display with the message I need to show to the user.

@Override
    public void onClick(View v) {

          new Thread(new Runnable() {
              @Override
              public void run() {

                  try {
                      loginDao = new LoginDao(databaseHelper.getConnectionSource());
                      login = loginDao.queryForId(1);
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
                  DadosCadastraisSerealizable dados = new DadosCadastraisSerealizable();


                  dados.codigo= String.valueOf(login.getCodigoCliente());
                  dados.usuario=login.usuario;
                  dados.senha=login.senha;

                  //expressão regular para enviar somente o numeros.
                  String[] cepText = edtCep.getText().toString().split("-");


                  dados.cep= cepText[0]+cepText[1];
                  dados.bairro= edtBairro.getText().toString();
                  dados.endereco= edtEndereco.getText().toString();
                  dados.numero= edtnumero.getText().toString();
                  dados.complemento= edtComplemento.getText().toString();
                  dados.foneComercial= edtFoneComercial.getText().toString();
                  dados.foneResidencial= edtFoneResidencial.getText().toString();
                  dados.foneCelular= edtFoneCelular.getText().toString();
                  dados.email= edtEmail.getText().toString();



                  try {
                      WebService ws = new WebService();

                      ws.atuzalizarCadastroCliente(dados);

                      Toast.makeText(getApplicationContext(), ws.strFault, Toast.LENGTH_SHORT).show();



                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (XmlPullParserException e) {
                      e.printStackTrace();
                  }



              }
          }).start();


    }

2 answers

2

First: The run method does not accept modifications to the interface from it..

Second: use Asynctask.

2


You can only modify the "screen" inside the thread main, the MainThread. Do it in the place you need it to work.

SuaAcitivity.this.runOnUiThread(new Runnable() {
    public void run() {
      Toast.makeText(getApplicationContext(), ws.strFault, Toast.LENGTH_SHORT).show();
    }
});

So the Toast is requested within the thread main without problem.

Browser other questions tagged

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