Search code examples
javaandroidnetworkonmainthread

Android - Java - NetworkOnMainThreadException


Whenever I try to send a packet, I am getting an Illegal State exception, but I don't know why it is happening as to the best of my knowledge, every variable is initialized and that is what generally causes an Illegal State Exception.

Here is the error:

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: [B@30365bc6 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

And here is the code that causes the error:

 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();
    }
}

Any clue as to what the problem is and how to fix it?


Solution

  • You are attempting a network operation on the main thread of your application. This is not allowed. See http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html