大家好,这是我在这里的第一个问题,所以请不要对我说残酷,我也要提前为我的坏英语抱歉。 所以我正在编写一个简单的应用程序,其工作是在指定的端口上发送和监听UDP协议,当我的应用程序通过主页或后退按钮最小化然后再返回到前面时,我的问题就来了。在它被带回后,我的应用程序抛出了由我的UDP接收器线程引起的EADDRINUSE异常:
public void run() {
try {
//Opening listening socket
Log.d("UDP Receiver", "Opening listening socket on port "+LISTENING_PORT+"...");
DatagramSocket socket = new DatagramSocket(LISTENING_PORT);
socket.setBroadcast(true);
socket.setReuseAddress(true);
while(true){
//Listening on socket
Log.d("UDP Receiver", "Listening...");
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
message_received = new String(packet.getData()).trim();
Log.d("UDP", "Received: '" + message_received + "'");
runOnUiThread(new Runnable() {
@Override
public void run() {
pokaztekst = (TextView) findViewById(R.id.textView);
pokaztekst.setText(message_received);
if(message_received.contains("192.168")){
String[] split_message = message_received.split("\"");
if (!listaurzadzen.toString().contains(split_message[3])) {
listaurzadzen.add(createPlanet("urzadzenie", split_message[3] + "\r\n" + split_message[1]));
lv.setAdapter(simpleAdpt);
}
}
}
});
}
} catch (Exception e) {
Log.e("UDP", "Receiver error", e);
}
}
}
我在onCreate方法中启动线程,如下所示:
Thread receiver = new Thread(new Receiver());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver.start();
此错误的后果是应该显示接收消息的listview字段和textview字段为空白。我希望有人可以帮助我。