Application crashes while starting the new thread

2019-08-30 08:07发布

问题:

I have a thread class when I start the thread and create the instance of thread class in the main class my app get crash. My main activity code for creating the thread is:

            broadcast broadcastobject=new broadcast(messages);
                broadcastobject.start();

My thread class is :

    public class broadcast extends Thread {

private DatagramSocket socket;
String str;
private static final int TIMEOUT_MS = 10;
WifiManager mWifi;
EditText et;
DatagramPacket packet;
Button bt;
private static final int SERVERPORT = 11111;
private static final String SERVER_IP = "192.168.1.255";

  public broadcast(String to) {
    // TODO Auto-generated constructor stub
     str = to;
}


/*
 private InetAddress getBroadcastAddress() throws IOException {
        DhcpInfo dhcp = mWifi.getDhcpInfo();
        if (dhcp == null) {
          //Log.d(TAG, "Could not get dhcp info");
          return null;
        }

        int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
        byte[] quads = new byte[4];
        for (int k = 0; k < 4; k++)
          quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
        return InetAddress.getByAddress(quads);
      }

 */
    @Override
    public void run() {


            try {
                socket = new DatagramSocket(SERVERPORT);
                socket.setBroadcast(true);
            } catch (SocketException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


        //              socket.setSoTimeout(TIMEOUT_MS);


            InetAddress serverAddr = null;

                try {
                    serverAddr =            InetAddress.getByName(SERVER_IP);
                } catch (UnknownHostException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }




        packet = new DatagramPacket(str.getBytes(), str.length(),serverAddr,SERVERPORT);


                    try {
                        socket.send(packet);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


    } 

    }

My log cat error is:

 09-01 08:23:47.949: D/gralloc_goldfish(1720): Emulator without GPU emulation detected.
 09-01 08:24:01.941: W/System.err(1720): java.net.SocketException: socket failed: EACCES (Permission denied)
 09-01 08:24:01.941: W/System.err(1720):    at libcore.io.IoBridge.socket(IoBridge.java:573)
 09-01 08:24:01.979: W/System.err(1720):    at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:91)
  09-01 08:24:01.979: W/System.err(1720):   at java.net.DatagramSocket.createSocket(DatagramSocket.java:131)
 09-01 08:24:01.979: W/System.err(1720):    at java.net.DatagramSocket.<init>(DatagramSocket.java:78)
  09-01 08:24:01.989: W/System.err(1720):   at soft.b.peopleassist.broadcast.run(broadcast.java:65)
  09-01 08:24:01.989: W/System.err(1720): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
  09-01 08:24:01.999: W/System.err(1720):   at libcore.io.Posix.socket(Native Method)
  09-01 08:24:01.999: W/System.err(1720):   at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:169)
  09-01 08:24:01.999: W/System.err(1720):   at libcore.io.IoBridge.socket(IoBridge.java:558)
 09-01 08:24:02.009: W/System.err(1720):    ... 4 more
 09-01 08:24:02.149: W/dalvikvm(1720): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
 09-01 08:24:02.149: E/AndroidRuntime(1720): FATAL EXCEPTION: Thread-114
  09-01 08:24:02.149: E/AndroidRuntime(1720): java.lang.NullPointerException
  09-01 08:24:02.149: E/AndroidRuntime(1720):   at soft.b.peopleassist.broadcast.run(broadcast.java:92)
09-01 08:24:03.329: W/IInputConnectionWrapper(1720): showStatusIcon on inactive InputConnection

回答1:

The thread is not the issue, the real issue is the EACCES (Permission denied), add this permission to your manifest file

<uses-permission android:name="android.permission.INTERNET"></uses-permission>