Android: сканирование Wi-Fi сети + список выбора
Я пытаюсь создать приложение, которое позволит вам сканировать доступные сети Wi-Fi, а затем отображать их в списке выбора. Для этого я попытался с этим кодом:
package android.nacho.WifiScan;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class WifiScan extends Activity {
TextView mainText;
WifiManager mainWifi;
WifiReceiver receiverWifi;
List<ScanResult> wifiList;
StringBuilder sb = new StringBuilder();
ListView listView = new ListView(this);
ArrayAdapter<String> adapter;
Context context = this;
static String[] NETWORKS;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi_scan);
mainText = (TextView) findViewById(R.id.text);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
mainText.setText("\\nStarting Scan...\\n");
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Refresh");
return super.onCreateOptionsMenu(menu);
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
mainWifi.startScan();
mainText.setText("Starting Scan");
return super.onMenuItemSelected(featureId, item);
}
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}
protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
wifiList = mainWifi.getScanResults();
NETWORKS = new String[wifiList.size()];
for(int i = 0; i < wifiList.size(); i++){
NETWORKS[i]=i+"-"+(wifiList.get(i)).toString();
}
System.out.println("debería imprimir: "+NETWORKS[2]);
adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_multiple_choice, NETWORKS);
// adapter.setDropDownViewResource(android.R.layout.select_dialog_multichoice);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
}
});
setContentView(listView);
}
}
}
Но я получаю это ошибки:
06-18 12:36:32.670: W/dalvikvm(15499): threadid=1: thread exiting with
uncaught exception (group=0x40dfb930)
06-18 12:36:32.670: E/AndroidRuntime(15499): FATAL EXCEPTION: main
06-18 12:36:32.670: E/AndroidRuntime(15499):
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{android.nacho.WifiScan/android.nacho.WifiScan.WifiScan}:
java.lang.NullPointerException
06-18 12:36:32.670: E/AndroidRuntime(15499): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
06-18 12:36:32.670: E/AndroidRuntime(15499): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-18 12:36:32.670: E/AndroidRuntime(15499): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-18 12:36:32.670: D/AndroidRuntime(15499): Shutting down VM 06-18
12:36:32.670: E/AndroidRuntime(15499): at
android.os.Looper.loop(Looper.java:137) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at
android.app.ActivityThread.main(ActivityThread.java:5039) 06-18
12:36:32.670: E/AndroidRuntime(15499): at
java.lang.reflect.Method.invokeNative(Native Method) 06-18
12:36:32.670: E/AndroidRuntime(15499): at
java.lang.reflect.Method.invoke(Method.java:511) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 12:36:32.670: E/AndroidRuntime(15499): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-18
12:36:32.670: E/AndroidRuntime(15499): at
dalvik.system.NativeStart.main(Native Method) 06-18 12:36:32.670:
E/AndroidRuntime(15499): Caused by: java.lang.NullPointerException
06-18 12:36:32.670: E/AndroidRuntime(15499): at
android.content.ContextWrapper.getResources(ContextWrapper.java:89)
06-18 12:36:32.670: E/AndroidRuntime(15499): at
android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
06-18 12:36:32.670: E/AndroidRuntime(15499): at
android.view.View.<init>(View.java:3224) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at android.view.View.<init>(View.java:3279)
06-18 12:36:32.670: E/AndroidRuntime(15499): at
android.view.ViewGroup.<init>(ViewGroup.java:431) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at
android.widget.AdapterView.<init>(AdapterView.java:235) 06-18
12:36:32.670: E/AndroidRuntime(15499): at
android.widget.AbsListView.<init>(AbsListView.java:766) 06-18
12:36:32.670: E/AndroidRuntime(15499): at
android.widget.ListView.<init>(ListView.java:143) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at
android.widget.ListView.<init>(ListView.java:139) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at
android.widget.ListView.<init>(ListView.java:135) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at
android.nacho.WifiScan.WifiScan.<init>(WifiScan.java:27) 06-18
12:36:32.670: E/AndroidRuntime(15499): at
java.lang.Class.newInstanceImpl(Native Method) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at
java.lang.Class.newInstance(Class.java:1319) 06-18 12:36:32.670:
E/AndroidRuntime(15499): at
android.app.Instrumentation.newActivity(Instrumentation.java:1054)
06-18 12:36:32.670: E/AndroidRuntime(15499): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
06-18 12:36:32.670: E/AndroidRuntime(15499): ... 11 more
Что я делаю неправильно??