我有这个方法来启动套接字连接,以便我可以在我的电脑和手机之间聊天。我的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
有谁知道这个问题可能是什么?
答案 0 :(得分:2)
读取抛出的异常中包含的错误!
Caused by: android.os.NetworkOnMainThreadException
由于您尝试在主UI线程上执行网络操作,因此抛出了异常。在Android中,您必须在UI线程之外执行任何耗时的任务(网络,数据库访问,冗长的数字运算等)。
您需要在单独的非UI线程上执行此操作。