title: Android: Scanning Wifi Network + Selectable list
body: <p>I'm trying to creat an application that let you scan the available wifi networks, and then display them in a selectable list. To do that I tried with this code:</p>
  
  <pre><code>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&lt;ScanResult&gt; wifiList;
      StringBuilder sb = new StringBuilder();
      ListView listView = new ListView(this);
      ArrayAdapter&lt;String&gt; 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 &lt; wifiList.size(); i++){
  
                  NETWORKS[i]=i+"-"+(wifiList.get(i)).toString();
  
  
              }
  
  
              System.out.println("debería imprimir: "+NETWORKS[2]);
  
  
              adapter = new ArrayAdapter&lt;String&gt;(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&lt;?&gt; arg0, View arg1, int arg2,long arg3) {
  
  
                  }
  
              });
  
              setContentView(listView);
  
          }
      }
  }
  </code></pre>
  
  <p>But I'm getting this errors:</p>
  
  <pre><code>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.&lt;init&gt;(View.java:3224) 06-18 12:36:32.670:
   E/AndroidRuntime(15499):   at android.view.View.&lt;init&gt;(View.java:3279)
   06-18 12:36:32.670: E/AndroidRuntime(15499):   at
   android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:431) 06-18 12:36:32.670:
   E/AndroidRuntime(15499):   at
   android.widget.AdapterView.&lt;init&gt;(AdapterView.java:235) 06-18
   12:36:32.670: E/AndroidRuntime(15499):     at
   android.widget.AbsListView.&lt;init&gt;(AbsListView.java:766) 06-18
   12:36:32.670: E/AndroidRuntime(15499):     at
   android.widget.ListView.&lt;init&gt;(ListView.java:143) 06-18 12:36:32.670:
   E/AndroidRuntime(15499):   at
   android.widget.ListView.&lt;init&gt;(ListView.java:139) 06-18 12:36:32.670:
   E/AndroidRuntime(15499):   at
   android.widget.ListView.&lt;init&gt;(ListView.java:135) 06-18 12:36:32.670:
   E/AndroidRuntime(15499):   at
   android.nacho.WifiScan.WifiScan.&lt;init&gt;(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
  </code></pre>
  
  <p>What am I doing wrong??</p>