gpt4 book ai didi

java - 尽管有 Try/Catch,Android 应用还是崩溃了

转载 作者:行者123 更新时间:2023-11-30 00:31:08 27 4
gpt4 key购买 nike

我正在尝试使用网络服务发现 (NSD) 编写应用程序,我注意到每当它崩溃时,错误行来自 try/catch 中的代码块。我无法指定捕获对于 SocketException 因为我相信(因为我尝试过) IOException 也涵盖了这一点。我该如何继续?

这是代码

class ServerThread implements Runnable {

@Override
public void run() {

try {
mServerSocket = new ServerSocket(0);
setLocalPort(mServerSocket.getLocalPort());

while (!Thread.currentThread().isInterrupted()) {
Log.d(TAG, "ServerSocket Created, awaiting connection");
setSocket(mServerSocket.accept()); //line of error
Log.d(TAG, "Connected.");
Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show();
if (mChatClient == null) {
int port = mSocket.getPort();
InetAddress address = mSocket.getInetAddress();
connectToServer(address, port);
}
}
} catch (IOException e) {
Log.e(TAG, "Error creating ServerSocket: ", e);
e.printStackTrace();
}
}
}

这是完整的错误日志

06-02 06:07:43.804 26779-26779/com.example.android.nsdchat E/MultiWindowProxy: getServiceInstance failed!
06-02 06:07:43.829 26779-26786/com.example.android.nsdchat E/art: Failed sending reply to debugger: Broken pipe
06-02 06:07:45.117 26779-26794/com.example.android.nsdchat E/ONS: 1. NsdChat (_http._tcp. -- 0 -- 0)
06-02 06:07:47.132 26779-26796/com.example.android.nsdchat E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9de01110
06-02 06:07:56.925 26779-26779/com.example.android.nsdchat E/IAE: listener in use
06-02 06:07:59.331 26779-26779/com.example.android.nsdchat E/IAE: listener in use
06-02 06:08:04.004 26779-26794/com.example.android.nsdchat E/NsdHelper: Resolve Succeeded. name: NsdChat, type: ._http._tcp, host: /192.168.82.103, port: 48308
06-02 06:08:05.995 26779-26796/com.example.android.nsdchat E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9da665c0
06-02 06:08:07.364 26779-26796/com.example.android.nsdchat E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9da6c990
06-02 06:08:07.716 26779-26793/com.example.android.nsdchat E/ChatConnection: Error creating ServerSocket:
java.net.SocketException: Socket closed
at libcore.io.Posix.accept(Native Method)
at libcore.io.BlockGuardOs.accept(BlockGuardOs.java:63)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:94)
at java.net.ServerSocket.implAccept(ServerSocket.java:217)
at java.net.ServerSocket.accept(ServerSocket.java:141)
at com.example.android.nsdchat.ChatConnection$ChatServer$ServerThread.run(ChatConnection.java:153)
at java.lang.Thread.run(Thread.java:818)
06-02 06:08:07.997 26779-26796/com.example.android.nsdchat E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9dcd2bd0

最佳答案

修复了!结果是错误行 setSocket(mServerSocket.accept());需要关闭此时为空的套接字。

关于java - 尽管有 Try/Catch,Android 应用还是崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320598/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com