0

I am trying to get a String from an EditText and emit it using socket.io, however, I keep getting the error android.view.ViewRootImpl$CalledFromWrongThreadException whenever this runs.

What I tried:

System.out.println("current status : " + socket.connected());
EditText send_message = findViewById(R.id.send_message);
String message = send_message.getText().toString();
MainActivity.this.runOnUiThread(() -> socket.emit("send_message", send_message.getText().toString()));

What happened:

E/AndroidRuntime: FATAL EXCEPTION: EventThread
    Process: dev.atomtables.atomchat, PID: 3719
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874)
        at android.view.View.requestLayout(View.java:17476)
        at android.view.View.requestLayout(View.java:17476)
        at android.view.View.requestLayout(View.java:17476)
        at android.view.View.requestLayout(View.java:17476)
        at android.view.View.requestLayout(View.java:17476)
        at android.widget.ScrollView.requestLayout(ScrollView.java:1483)
        at android.view.View.requestLayout(View.java:17476)
        at android.view.ViewGroup.addView(ViewGroup.java:3784)
        at android.view.ViewGroup.addView(ViewGroup.java:3727)
        at android.view.ViewGroup.addView(ViewGroup.java:3700)
        at dev.atomtables.atomchat.MainActivity.onReceiveMessage(MainActivity.java:279)
        at dev.atomtables.atomchat.MainActivity.lambda$onCreate$4$dev-atomtables-atomchat-MainActivity(MainActivity.java:98)
        at dev.atomtables.atomchat.MainActivity$$ExternalSyntheticLambda5.call(D8$$SyntheticClass)
        at io.socket.emitter.Emitter.emit(Emitter.java:117)
        at io.socket.client.Socket.onevent(Socket.java:363)
        at io.socket.client.Socket.onpacket(Socket.java:307)
        at io.socket.client.Socket.access$100(Socket.java:19)
        at io.socket.client.Socket$2$2.call(Socket.java:92)
        at io.socket.emitter.Emitter.emit(Emitter.java:117)
        at io.socket.client.Manager.ondecoded(Manager.java:361)
        at io.socket.client.Manager.access$1200(Manager.java:22)
        at io.socket.client.Manager$5.call(Manager.java:355)
        at io.socket.parser.IOParser$Decoder.add(IOParser.java:103)
        at io.socket.client.Manager$2.call(Manager.java:331)
        at io.socket.emitter.Emitter.emit(Emitter.java:117)
        at io.socket.engineio.client.Socket.onPacket(Socket.java:556)
        at io.socket.engineio.client.Socket.access$1000(Socket.java:29)
        at io.socket.engineio.client.Socket$5.call(Socket.java:335)

1 Answer 1

0

The problem isn't here. Emit causes it to send a message. That message is emitted on a thread chosen by your socket library. So you're calling MainActivity.onRecieveMessage on the socket's thread. You need the runOnUiThread in onRecieveMessage, not here. It's perfectly fine to emit from any thread.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.