我有这种方法来启动套接字连接,以便我可以在我的 PC 和电话之间聊天。我的 PC 上已经有一台服务器正在等待来自 Android 的连接。
private void connectToServer() throws IOException
{
showMessage("Starting Connection...");
connection = new Socket(InetAddress.getByName(serverIP), 0511);
showMessage("\n Connection Established: "+connection.getInetAddress().getHostName());
}
这是我运行它时遇到的错误:
03-08 19:02:36.847: E/AndroidRuntime(2524): FATAL EXCEPTION: main
03-08 19:02:36.847: E/AndroidRuntime(2524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jister13.chattest/com.jister13.chattest.ChatRoom}: android.os.NetworkOnMainThreadException
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.app.ActivityThread.access$700(ActivityThread.java:139)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.os.Looper.loop(Looper.java:137)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.app.ActivityThread.main(ActivityThread.java:4918)
03-08 19:02:36.847: E/AndroidRuntime(2524): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 19:02:36.847: E/AndroidRuntime(2524): at java.lang.reflect.Method.invoke(Method.java:511)
03-08 19:02:36.847: E/AndroidRuntime(2524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
03-08 19:02:36.847: E/AndroidRuntime(2524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
03-08 19:02:36.847: E/AndroidRuntime(2524): at dalvik.system.NativeStart.main(Native Method)
03-08 19:02:36.847: E/AndroidRuntime(2524): Caused by: android.os.NetworkOnMainThreadException
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
03-08 19:02:36.847: E/AndroidRuntime(2524): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
03-08 19:02:36.847: E/AndroidRuntime(2524): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
03-08 19:02:36.847: E/AndroidRuntime(2524): at libcore.io.IoBridge.connect(IoBridge.java:112)
03-08 19:02:36.847: E/AndroidRuntime(2524): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-08 19:02:36.847: E/AndroidRuntime(2524): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
03-08 19:02:36.847: E/AndroidRuntime(2524): at java.net.Socket.startupSocket(Socket.java:565)
03-08 19:02:36.847: E/AndroidRuntime(2524): at java.net.Socket.<init>(Socket.java:225)
03-08 19:02:36.847: E/AndroidRuntime(2524): at com.jister13.chattest.ChatRoom.connectToServer(ChatRoom.java:88)
03-08 19:02:36.847: E/AndroidRuntime(2524): at com.jister13.chattest.ChatRoom.startRunning(ChatRoom.java:74)
03-08 19:02:36.847: E/AndroidRuntime(2524): at com.jister13.chattest.ChatRoom.onCreate(ChatRoom.java:69)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.app.Activity.performCreate(Activity.java:5048)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-08 19:02:36.847: E/AndroidRuntime(2524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052)
03-08 19:02:36.847: E/AndroidRuntime(2524): ... 11 more
有谁知道问题可能是什么?