我编写了一个客户端应用程序,其中我使用了InetAddress.getByName。当我在模拟器(目标平台是2.2)或Archos Internet Tablet(Android 2.2)上运行应用程序时,它运行正常。但是当我尝试在我的Asus MeMo Pad(Android 4.2)上运行该应用程序时,该应用程序会抛出异常。我能做什么?我应该用目标4.2创建一个新项目还是更改manifest.xml?
我有一个连接服务器的程序:
static void CreateNewSocket(){
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
在我调用InetAddress.getByName(SERVER_IP)的行中 执行停止,我可以在平板电脑上看到"" GastroOrder" wurde beendet"。此消息是德语,表示应用程序已终止。 在主线程中调用CreateNewSocket()。 我认为问题是因为android.os.NetworkOnMainThreadException。 我该如何解决这个问题?是否可以在不为套接字连接创建单独线程的情况下解决此问题?
logcat的:
12-24 18:08:16.641:D / jdwp(14019):processIncoming
12-24 18:08:16.641:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0x9,len = 0x13,id = 0x37D,flags = 0x0,dataLen = 0x8
12-24 18:08:16.642:D / jdwp(14019):processIncoming
12-24 18:08:16.642:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0x6,len = 0x17,id = 0x37E,flags = 0x0,dataLen = 0xC
12-24 18:08:16.643:D / jdwp(14019):processIncoming
12-24 18:08:16.644:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0xB,len = 0x13,id = 0x37F,flags = 0x0,dataLen = 0x8
12-24 18:08:16.686:D / jdwp(14019):processIncoming
12-24 18:08:16.686:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0xB,len = 0x13,id = 0x380,flags = 0x0,dataLen = 0x8
12-24 18:08:16.693:D / jdwp(14019):processIncoming
12-24 18:08:16.693:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0xB,len = 0x13,id = 0x381,flags = 0x0,dataLen = 0x8
12-24 18:08:16.701:D / jdwp(14019):processIncoming
12-24 18:08:16.701:D / jdwp(14019):handlePacket:cmd = 0x7,cmdSet = 0xB,len = 0x13,id = 0x382,flags = 0x0,dataLen = 0x8
12-24 18:08:16.702:D / jdwp(14019):processIncoming
12-24 18:08:16.702:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0xB,len = 0x13,id = 0x383,flags = 0x0,dataLen = 0x8
12-24 18:08:16.750:D / jdwp(14019):processIncoming
12-24 18:08:16.750:D / jdwp(14019):handlePacket:cmd = 0x7,cmdSet = 0xB,len = 0x13,id = 0x384,flags = 0x0,dataLen = 0x8
12-24 18:08:16.752:D / jdwp(14019):processIncoming
12-24 18:08:16.752:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0xB,len = 0x13,id = 0x385,flags = 0x0,dataLen = 0x8
12-24 18:08:16.756:D / jdwp(14019):processIncoming
12-24 18:08:16.756:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0x2,len = 0x13,id = 0x386,flags = 0x0,dataLen = 0x8
12-24 18:08:16.757:D / jdwp(14019):processIncoming
12-24 18:08:16.757:D / jdwp(14019):handlePacket:cmd = 0xD,cmdSet = 0x2,len = 0x13,id = 0x387,flags = 0x0,dataLen = 0x8
12-24 18:08:16.758:D / jdwp(14019):processIncoming
12-24 18:08:16.758:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0x6,len = 0x17,id = 0x388,flags = 0x0,dataLen = 0xC
12-24 18:08:16.759:D / jdwp(14019):processIncoming
12-24 18:08:16.759:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0x2,len = 0x13,id = 0x389,flags = 0x0,dataLen = 0x8
12-24 18:08:16.760:D / jdwp(14019):processIncoming
12-24 18:08:16.760:D / jdwp(14019):handlePacket:cmd = 0xD,cmdSet = 0x2,len = 0x13,id = 0x38A,flags = 0x0,dataLen = 0x8
12-24 18:08:17.338:D / jdwp(14019):processIncoming
12-24 18:08:17.338:D / jdwp(14019):handlePacket:cmd = 0x3,cmdSet = 0xB,len = 0x13,id = 0x38B,flags = 0x0,dataLen = 0x8
12-24 18:08:17.338:D / AndroidRuntime(14019):关闭VM
12-24 18:08:17.338:W / dalvikvm(14019):threadid = 1:线程退出未捕获异常(组= 0x416bc9a8)
12-24 18:08:17.349:D / jdwp(14019):processIncoming
12-24 18:08:17.349:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0xB,len = 0x13,id = 0x38C,flags = 0x0,dataLen = 0x8
12-24 18:08:17.350:D / jdwp(14019):processIncoming
12-24 18:08:17.350:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0xB,len = 0x13,id = 0x38D,flags = 0x0,dataLen = 0x8
12-24 18:08:17.352:D / jdwp(14019):processIncoming
12-24 18:08:17.352:D / jdwp(14019):handlePacket:cmd = 0x1,cmdSet = 0xB,len = 0x13,id = 0x38E,flags = 0x0,dataLen = 0x8
12-24 18:08:17.366:E / AndroidRuntime(14019):致命异常:主要
12-24 18:08:17.366:E / AndroidRuntime(14019):java.lang.RuntimeException:无法启动活动ComponentInfo {com.myprojects.gastroorder / com.myprojects.gastroorder.MainActivity}:android.os.NetworkOnMainThreadException
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.app.ActivityThread.access $ 600(ActivityThread.java:156)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1340)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.os.Handler.dispatchMessage(Handler.java:99)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.os.Looper.loop(Looper.java:153)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.app.ActivityThread.main(ActivityThread.java:5299)
12-24 18:08:17.366:E / AndroidRuntime(14019):at java.lang.reflect.Method.invokeNative(Native Method)
12-24 18:08:17.366:E / AndroidRuntime(14019):at java.lang.reflect.Method.invoke(Method.java:511)
12-24 18:08:17.366:E / AndroidRuntime(14019):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)
12-24 18:08:17.366:E / AndroidRuntime(14019):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-24 18:08:17.366:E / AndroidRuntime(14019):at dalvik.system.NativeStart.main(Native Method)
12-24 18:08:17.366:E / AndroidRuntime(14019):引起:android.os.NetworkOnMainThreadException
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
12-24 18:08:17.366:E / AndroidRuntime(14019):at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-24 18:08:17.366:E / AndroidRuntime(14019):at libcore.io.IoBridge.connectErrno(IoBridge.java:141)
12-24 18:08:17.366:E / AndroidRuntime(14019):at libcore.io.IoBridge.connect(IoBridge.java:112)
12-24 18:08:17.366:E / AndroidRuntime(14019):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-24 18:08:17.366:E / AndroidRuntime(14019):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
12-24 18:08:17.366:E / AndroidRuntime(14019):在java.net.Socket.startupSocket(Socket.java:575)
12-24 18:08:17.366:E / AndroidRuntime(14019):at java.net.Socket。(Socket.java:228)
12-24 18:08:17.366:E / AndroidRuntime(14019):at com.myprojects.gastroorder.MainActivity.CreateNewSocket(MainActivity.java:747)
12-24 18:08:17.366:E / AndroidRuntime(14019):at com.myprojects.gastroorder.MainActivity.onCreate(MainActivity.java:240)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.app.Activity.performCreate(Activity.java:5122)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
12-24 18:08:17.366:E / AndroidRuntime(14019):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-24 18:08:17.366:E / AndroidRuntime(14019):... 11更多
12-24 18:08:17.398:I / Process(14019):发送信号。 PID:14019 SIG:9