Toast is not displayed [duplicate]

1

I have a problem, Toast does not appear when I make a request in to my web service, the whole method stays inside my thread, I made a test putting a message out of the thread it appears, but well 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();


    }
    
asked by anonymous 08.12.2016 / 18:29

2 answers

2

You can only change the "screen" within thread master, MainThread . Do this where you need it to work.

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

So Toast is requested within thread main without problem.

    
08.12.2016 / 18:47
2

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

Second: use AsyncTask.

    
08.12.2016 / 19:03