0

这段代码给出了一个错误,说“在活动中接收广播意图时出错”我找不到任何错误…………有什么想法吗?我已将循环条件添加为七个,因为我只想要前七个扫描结果

class WifiReceiver extends BroadcastReceiver {
    public void onReceive(Context con, Intent intent) {
        sb = new StringBuilder();
        wifiList = mainWifi.getScanResults();

        for(int i = 0; i < wifiList.size(); i++)
        {
            sb.append((wifiList.get(i)).SSID.toString());
            sb.append(' ');
            sb.append('!');
            sb.append("\n\n");
        }

        String net = sb.toString();

        if(wifiList.size() > 0)
        {


        char excl = '!';
        int excl1 = excl;
        String[] aray = null;
        for(int j = 0; j<7; j++)
        {
            int index = net.indexOf(excl1);
            String a = net.substring(0, index);
            aray[j] = a;
            String temp = net.substring(index+1);
            net = temp;

        }

        String one = aray[0];
        String two = aray[1];
        String three = aray[2];
        String four = aray[3];
        String five = aray[4];
        String six = aray[5];
        String seven = aray[7];
        tv1.setText(one);

    }
        else
        {
            tv1.setText("No Networks Detected");
        }
}
}

PS:我只添加到一个 TextView 因为这是一个测试模块

4

1 回答 1

0

“aray”数组未正确初始化。您需要在访问它之前初始化“aray”:

String[] aray = new String[7];

您还将超出数组的维度。这:

String seven = aray[7];

应该:

String seven = aray[6];

更好的解决方案是使用 ArrayList。

ArrayList al = new ArrayList();
al.add(yourString);
于 2012-05-29T15:40:34.923 回答