UDP接收函数抛出nullpointerexception

时间:2014-01-15 12:09:57

标签: android udp

我正在尝试通过wifi网络发送和接收简单的UDP消息。我使用以下代码。我的wifi网络工作正常,我已初始化套接字变量然后每次我在接收方法得到空指针异常。请帮忙。

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

1 个答案:

答案 0 :(得分:0)

或者:

  1. 'serverSocketUDP'必须为null
  2. 您的描述不正确,或
  3. 这不是真正的代码。