2014-01-15 18 views
-1

我想通過無線網絡發送和接收簡單的UDP信息。我正在使用下面的代碼。我的wifi網絡工作正常,我已初始化套接字變量然後每次我都收到空指針異常接收方法。請幫忙。UDP接收函數拋出nullpointerexception

public void run() { 
      // TODO Auto-generated method stub 
      WifiConfiguration conf=new WifiConfiguration(); 
       String networkSSID="Lenovo S920"; 
       String pass="12345678saz"; 
       conf.SSID = "\"" + networkSSID + "\""; 
       conf.preSharedKey = "\"" + pass + "\""; 
       conf.status = WifiConfiguration.Status.ENABLED;   
       conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
       conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
       conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
       conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
       conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
       conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
      byte[] buf = new byte[1024]; 
      DatagramPacket packet = new DatagramPacket(buf, buf.length); 
      try { 
       serverSocketUDP.receive(packet); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      catch(NetworkOnMainThreadException e) { 
       e.printStackTrace(); 
      } 
+3

如何在何處初始化serverSocketUDP?這是缺少的相關代碼。 – Peter

回答

0

或者:

  1. 'serverSocketUDP' 必須爲空
  2. 你的描述是不正確,或
  3. 這不是真正的代碼。
相關問題