我面临一些困难,包括我的应用程序中的WIFI代码。除非我已经尝试了所有内容,否则我不会发布任何问题,我唯一怀疑的可能是因为WIFI上市。
以下代码添加到OnCreat函数之外的MainActivity中。
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();//getting the scan result into a list
//........................................................
wifis = new String[wifiScanList.size()];
for(int i = 0; i < wifiScanList.size(); i++){
wifis[i] = ((wifiScanList.get(i).BSSID).toString()+" , "
+ String.valueOf(wifiScanList.get(i).level) +" , " + String.valueOf(wifiScanList.get(i).frequency));
Log.e("WIFI", " : " + wifis[i]);
//........................................................
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,wifis));
它单独运行很好,但是当我尝试运行并且它强制关闭我的App时,我收到以下错误。
03-30 21:19:45.206: E/AndroidRuntime(8530): FATAL EXCEPTION: main
03-30 21:19:45.206: E/AndroidRuntime(8530): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x8000010 } in ash.email.MainActivity$WifiScanReceiver@429f8288
03-30 21:19:45.206: E/AndroidRuntime(8530): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
03-30 21:19:45.206: E/AndroidRuntime(8530): at android.os.Handler.handleCallback(Handler.java:730)
03-30 21:19:45.206: E/AndroidRuntime(8530): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 21:19:45.206: E/AndroidRuntime(8530): at android.os.Looper.loop(Looper.java:137)
03-30 21:19:45.206: E/AndroidRuntime(8530): at android.app.ActivityThread.main(ActivityThread.java:5493)
03-30 21:19:45.206: E/AndroidRuntime(8530): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 21:19:45.206: E/AndroidRuntime(8530): Caused by: java.lang.NullPointerException
03-30 21:53:53.436: E/AndroidRuntime(10258): at ash.email.MainActivity$WifiScanReceiver.onReceive(MainActivity.java:252)
03-30 21:53:53.436: E/AndroidRuntime(10258): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
03-30 21:53:53.436: E/AndroidRuntime(10258): ... 9 more