Method being called several times

Asked

Viewed 62 times

0

I have a little problem. The method of adding Message is being called several times and keeps adding messages until the app is closed.I have no idea what’s causing this,!

Fragment containing the Send button:

public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedinstance)
{
    final View view = inflater.inflate(R.layout.fragment_chat_layout,null);


    Button EnviarMensagem = (Button)view.findViewById(R.id.ButtonEnviarMensagem);
    EnviarMensagem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText msg = (EditText)view.findViewById(R.id.EditTextMensagem);
            try {
                Controle_Chat.AdicionarMensagem(msg.getText().toString());
                Log.i("FragmentChat","Clicou em enviar mensagem");
            } catch (IOException e) {
                e.printStackTrace();
            }
            msg.setText("");
        }
    });

Controle_chat:

  public static void AdicionarMensagem(String mensagem) throws IOException {


    Log.i("AdicionarMensagem","Entrou metodo Adicionar Mensagem");
    if (activity.cliente != null) {
        activity.cliente.EnviarOpcao(String.valueOf("chat:"+mensagem));
    } else {
        activity.servidor.EnviarCliente(String.valueOf("chat:"+mensagem));

    }

    mensagens.add(mensagem);


    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            FragmentChat.adapter.notifyDataSetChanged();

        }
    });

Socket control, when client receives message:

  BufferedReader ler = new BufferedReader(new InputStreamReader(servidor.getInputStream()));
        String opcao;

        while((opcao=ler.readLine()) !=null)
        {
Log.i("OPCAO CLIENTE",opcao);

            if(opcao.equals("99"))
            {

                activity.ResetarJogo();
            }else if(opcao.contains("chat")){

                String[] cortada = opcao.split(":");
                Controle_Chat.AdicionarMensagem(cortada[1]);
                Log.i("Cliente","Entrou no if se tem mensagem");
            }else{
                activity.controle_jogo.SelecionarRemoto(Integer.valueOf(opcao));
                activity.controle_jogo.SetMinhaVez(true);
            }



        }

Method is only in these places,.

The console output:

Saída

Can anyone give an idea of what might be going on? Thank you!

1 answer

2

from what I’ve seen, you use verification activity.cliente != nullto send messages, but I didn’t see you go back to activity.cliente = null after sending and thus finishing the loop.

hope it helps.

  • Hi, thanks for the help,I solved the problem otherwise, because I could not set null to the client variable I had to create a method to send and call this method in the clicklistener button,putting this if/Else inside another method,now why I don’t know haha anymore. Hugs!

Browser other questions tagged

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