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:
Can anyone give an idea of what might be going on? Thank you!
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!
– saidmrn