我有這種方法來啓動套接字連接,以便我可以在我的PC和手機之間聊天。我的電腦上已經有一臺等待與Android連接的服務器。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
有誰知道這個問題可能是什麼?
當您搜索該錯誤時,StackOverflow上數百個匹配問題中的任何一個沒有幫助? – 2013-03-09 00:07:43