2013-08-31 71 views
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 
+0

這與線程無關。你在該文件的第92行獲得NPE。我懷疑也許str是空的?很難知道,不知道92線在哪裏。 – Gray

+0

你可以花一些時間修整代碼,刪除塊註釋,並重新格式化嗎? – Gray

回答

0

線程沒有問題,真正的問題是EACCES (Permission denied),將此權限添加到您的清單文件

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

沒有它沒有問題。 – Nerd