Method being called several times

0

I have a problem. The method of adding Message is being called several times and is adding messages until the application is closed. I have no idea what is causing this, I will put the sending parts, if anyone knows how to help , thank you!

Fragment that contains the Submit 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("");
        }
    });

ControlChat:

  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 the client receives a 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);
            }



        }

The method is only in these places, when it receives a message or when it sends.

Console output:

Can anyone give an idea of what might be happening? Thanks!

    
asked by anonymous 23.11.2016 / 20:39

1 answer

2

From what I saw, you use the activity.cliente != null check to be able to send the messages, but I did not see you go back to activity.cliente = null after sending and thus to end the loop.

I hope it helps.

    
23.11.2016 / 23:51