2015-11-30 2 views
0

Всякий раз, когда я пытаюсь отправить пакет, я получаю исключение из нелегального состояния, но я не знаю, почему это происходит, насколько мне известно, каждая переменная инициализирована, и это то, что как правило, вызывает незаконное исключение государства.Android - Java - NetworkOnMainThreadException

Здесь ошибка:

W/myApp: Reached Function 
D/libc-netbsd: [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0 
D/libc: getaddrinfo called from pid =1013 
D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=4; ai_family=0 
W/myApp: 1 converted to bytes 
W/myApp: Packet created: Send to: 144.118.119.31 Send: [[email protected] 1 1 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
E/AndroidRuntime: Process: com.example.cody.lighttest, PID: 1013 
E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity 
E/AndroidRuntime:  at android.view.View$1.onClick(View.java:4015) 
E/AndroidRuntime:  at android.view.View.performClick(View.java:4764) 
E/AndroidRuntime:  at android.view.View$PerformClick.run(View.java:19844) 
E/AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:739) 
E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:95) 
E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:135) 
E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5351) 
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372) 
E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException 
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372) 
E/AndroidRuntime:  at android.view.View$1.onClick(View.java:4010) 
E/AndroidRuntime:  at android.view.View.performClick(View.java:4764)  
E/AndroidRuntime:  at android.view.View$PerformClick.run(View.java:19844)  
E/AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:739)  
E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:95)  
E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:135)  
E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5351)  
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372)  
E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)  
E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)  
E/AndroidRuntime: Caused by: android.os.NetworkOnMainThreadException 
E/AndroidRuntime:  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 
E/AndroidRuntime:  at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:276) 
E/AndroidRuntime:  at libcore.io.IoBridge.sendto(IoBridge.java:513) 
E/AndroidRuntime:  at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:184) 
E/AndroidRuntime:  at java.net.DatagramSocket.send(DatagramSocket.java:305) 
E/AndroidRuntime:  at com.example.cody.lighttest.MainActivity.sendMessage(MainActivity.java:72) 
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372)  
E/AndroidRuntime:  at android.view.View$1.onClick(View.java:4010)  
E/AndroidRuntime:  at android.view.View.performClick(View.java:4764)  
E/AndroidRuntime:  at android.view.View$PerformClick.run(View.java:19844)  
E/AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:739)  
E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:95)  
E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:135)  
E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5351)  
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372)  
E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)  
11-30 16:00:49.259 1013-1013/com.example.cody.lighttest E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)  
11-30 16:00:51.649 1013-1013/? I/Process: Sending signal. PID: 1013 SIG: 9 

А вот код, который вызывает ошибку:

public void sendMessage(View view) { 
     // Send UDP message to server 
     Log.w("myApp", "Reached Function"); 
     try { 
      InetAddress IPAddress = InetAddress.getByName("144.118.119.31"); 
      String toSend = "1"; 
      byte [] buf = toSend.getBytes(); 
      Log.w("myApp", "1 converted to bytes"); 
      DatagramPacket packet = new DatagramPacket(buf, toSend.length(), IPAddress, 5005); 
      Log.w("myApp", "Packet created: Send to: " + IPAddress.getHostAddress() + " Send: " + buf.toString() + " " + buf.length + " " + toSend.length()); 
      DatagramSocket csock = new DatagramSocket(5005); 
      csock.send(packet); 
      Log.w("myApp", "Sent Packet"); 

    } catch (SocketException e) { 
     e.printStackTrace(); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

Любой ключ к пониманию того, что проблема и как это исправить?

ответ

 Смежные вопросы

  • Нет связанных вопросов^_^