Hi I'm trying to develop a simple chat for Android, I'm using as Node.js server, the socket library I'm using is compile 'com.github.nkzawa:socket.io-client:0.4.0'
So far I have managed to make the connection and run the events to issue and listen events, but I have a problem, not because when you run one of the methods event listener and try to modify the interface of the fragment where messages are displayed the application stops and exits.
Event Listener new posts
socket.on(EVENT_SEND_GET_MESSAGE, new Emitter.Listener(){
@Override
public void call(Object... args) {
JSONObject Mensaje = (JSONObject) args[0];
if ( !Mensaje.equals("") && CONTENTCHAT != null ){
try {
if( Mensaje.getString("Remitente").equals("7") ){
CONTENTCHAT.addView( GenerarTextView(0, Mensaje.getString("Mensaje") ) );
}else{
CONTENTCHAT.addView( GenerarTextView(1, Mensaje.getString("Mensaje") ) );
}
Log.i("RESPONSE", Mensaje.getString("Mensaje"));
} catch (JSONException e) {
e.printStackTrace();
}
//Log.i("RESPONSE", args[0].toString());
}
}
});
If I comment the following code and only sending a log, it runs smoothly.
CONTENTCHAT.addView( GenerarTextView(1, Mensaje.getString("Mensaje") ) );
The function GenerarTextView it's
private TextView GenerarTextView(int origen, String mensaje){//generar textview
TextView texto = new TextView(CONTEXTO);
if( origen == 0){
texto.setBackgroundResource(R.color.accent_transparency_remitente);
texto.setGravity(Gravity.LEFT);
}else{
texto.setBackgroundResource(R.color.accent_transparency_receptor);
texto.setGravity(Gravity.RIGHT);
}
texto.setTextColor(CONTEXTO.getResources().getColor(R.color.textBlack));
texto.setPadding(30, 5, 30, 5);
texto.setTextSize(15);
texto.setText(mensaje);
return texto;
}
I know it's not an error when generating TextViews because if I add the outgoing message adds smoothly, it is only when I try to do when the event is triggered.
I appreciate any help please.
This it's error show in console
05-15 18:07:15.458 27843-28205/com.co.edu.cun.www1104379214.bienestarcun E/AndroidRuntime: FATAL EXCEPTION: EventThread
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.widget.Toast$TN.<init>(Toast.java:322)
at android.widget.Toast.<init>(Toast.java:91)
at android.widget.Toast.makeText(Toast.java:238)
at com.co.edu.cun.www1104379214.bienestarcun.Funciones.ChatPsicologiaManager.AddTextHistoryChat(ChatPsicologiaManager.java:140)
at com.co.edu.cun.www1104379214.bienestarcun.Funciones.ChatPsicologiaManager.access$000(ChatPsicologiaManager.java:36)
at com.co.edu.cun.www1104379214.bienestarcun.Funciones.ChatPsicologiaManager$1.call(ChatPsicologiaManager.java:90)
at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)
at com.github.nkzawa.socketio.client.Socket.onevent(Socket.java:316)
at com.github.nkzawa.socketio.client.Socket.onpacket(Socket.java:280)
at com.github.nkzawa.socketio.client.Socket.access$100(Socket.java:18)
at com.github.nkzawa.socketio.client.Socket$2$2.call(Socket.java:101)
at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)
at com.github.nkzawa.socketio.client.Manager.ondecoded(Manager.java:379)
at com.github.nkzawa.socketio.client.Manager.access$1300(Manager.java:20)
at com.github.nkzawa.socketio.client.Manager$3.call(Manager.java:353)
at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)
at com.github.nkzawa.socketio.parser.Parser$Decoder.add(Parser.java:156)
at com.github.nkzawa.socketio.client.Manager.ondata(Manager.java:371)
at com.github.nkzawa.socketio.client.Manager.access$1100(Manager.java:20)
at com.github.nkzawa.socketio.client.Manager$2.call(Manager.java:344)
at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)
at com.github.nkzawa.engineio.client.Socket.onPacket(Socket.java:485)
at com.github.nkzawa.engineio.client.Socket.access$900(Socket.java:29)
at com.github.nkzawa.engineio.client.Socket$5.call(Socket.java:288)
at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)
at com.github.nkzawa.engineio.client.Transport.onPacket(Transport.java:121)
at com.github.nkzawa.engineio.client.Transport.onData(Transport.java:113)
at com.github.nkzawa.engineio.client.transports.WebSocket.access$100(WebSocket.java:19)
at com.github.nkzawa.engineio.client.transports.WebSocket$1$3.run(WebSocket.java:74)
at com.github.nkzawa.thread.EventThread$2.run(EventThread.java:75)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)