0

我第一次启用 Wifi 然后禁用它一切都很好,但是如果我尝试在之后(立即或等待移动数据再次连接)重新启用 Wifi,它会抛出 NullPointerException 并强制关闭。

private void toggleWifi(){

    if (wifi == 0){
        wifiManager.setWifiEnabled(true);
        scanOnly = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "scanOnly");
        scanOnly.acquire();

        bWifi.setText("Turn Wifi OFF");

        List<ScanResult> wifiResults = wifiManager.getScanResults();

        StringBuilder sb = new StringBuilder("Scan Results:\n");            
        sb.append("-----------------------\n");

        for (ScanResult r : wifiResults) {
            sb.append(r.SSID + " " + r.level + " dBM\n");
        }

        tvWifi.setText(sb.toString());

        wifi = 1;

    } else {
        scanOnly.release();
        wifiManager.setWifiEnabled(false);

        bWifi.setText("Turn WiFi ON");

        tvWifi.setText("");

        wifi = 0;
    }

}

错误在这一行:

for (ScanResult r : wifiResults) {
    sb.append(r.SSID + " " + r.level + " dBM\n");
}
4

1 回答 1

0

你试过调试吗?目前 wifiResults 可能为空

于 2012-04-05T01:46:59.663 回答