0

当我启动线程并在我的应用程序崩溃的主类中创建线程类的实例时,我有一个线程类。我创建线程的主要活动代码是:

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

我的线程类是:

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


    } 

    }

我的日志猫错误是:

 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
4

1 回答 1

0

线程不是问题,真正的问题是EACCES (Permission denied),将此权限添加到您的清单文件

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
于 2013-09-01T04:41:57.057 回答