Android $ calledfromwrongthreadeexception错误时,试图获得字符串发送到socke



我正试图从EditText获得一个字符串,并使用套接字发出它。然而,我一直得到错误android.view。ViewRootImpl$ callledfromwrongthreadeexception

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()));

事件:

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)

问题不在这里。Emit使其发送消息。该消息在套接字库选择的线程上发出。你在调用MainActivity。socket线程上的onreceievemessage。你需要在onRecieveMessage中使用runonuthread,而不是在这里。从任何线程发出都是完全可以的。

相关内容

  • 没有找到相关文章

最新更新