title: Using Tabs,Wifi and Gps
body: <p>Am Generating 3 tabs dynamically under 1st tab am generating a toggle button using which i can switch off and on both GPS and WIFI<br>
  I have coded java files for GPS and WIFI. I have checked again and again line by line but couldn't find any bug.
  I have run in the emulator and android "Sony Arc mobile also."Installation also goes well and problem arises when i try to launch.
  The application is showing the message "<strong>The application has stopped unexpectedly please try again</strong>" I have referred almost{<em>all</em>} all the queries in stack overflow and couldn't get any possible answer for my scenario..
  I tried removing the GPS and WIFI files in the application and replacing <em>Toggle Button</em> with <em>TextView</em> then its working fine.<br>
    Am including my code please verify it and help me out buddies.  </p>
  
  <blockquote>
    <p><strong>Main.java</strong>  </p>
    
    <blockquote>
      <p>package project.sow;<br>
      import android.app.TabActivity;<br>
      import android.content.Intent;<br>
      import android.content.res.Resources;<br>
      import android.os.Bundle;<br>
      import android.widget.TabHost;<br>
      public class Main extends TabActivity {<br>
          private TabHost tabHost;<br>
          @Override<br>
          public void onCreate(Bundle savedInstanceState) {<br>
              try{<br>
              super.onCreate(savedInstanceState);<br>
              setContentView(R.layout.main);<br>
              Resources res = getResources(); // Resource object to get Drawables<br>
              tabHost = (TabHost)findViewById(android.R.id.tabhost); //The activity TabHost<br>
              TabHost.TabSpec spec;  // Resusable TabSpec for each tab<br>
                       // Initialize a TabSpec for each tab and add it to the TabHost<br>
              spec = tabHost.newTabSpec("Start").setIndicator("Start",
                                res.getDrawable(R.drawable.ic_sample));<br>
              spec.setContent(new Intent(this,Start.class));<br>
              tabHost.addTab(spec);<br>
              spec = tabHost.newTabSpec("Info").setIndicator("Info",   res.getDrawable(R.drawable.ic_sample));<br>
              spec.setContent(new Intent().setClass(this, Info.class));<br>
              tabHost.addTab(spec);  </p>
    </blockquote>
  </blockquote>
  
  <pre><code>   spec = tabHost.newTabSpec("Update").setIndicator("Update",   res.getDrawable(R.drawable.ic_sample));             
      spec.setContent(new Intent(this, Update.class));  
      tabHost.addTab(spec);  
  
      tabHost.setCurrentTab(1);  
  }catch(Exception e)  
  {
      android.util.Log.i("PREFS",e.getMessage());  
  } } }     
  </code></pre>
  
  <blockquote>
    <p><strong>Start.java</strong>    </p>
    
    <blockquote>
      <p>package project.sow;<br>
      import android.app.Activity;<br>
      import android.content.BroadcastReceiver;<br>
      import android.content.Context;<br>
      import android.content.IntentFilter;<br>
      import android.location.LocationListener;<br>
      import android.location.LocationManager;<br>
      import android.location.LocationProvider;<br>
      import android.net.wifi.WifiManager;<br>
      import android.os.Bundle;<br>
      import android.widget.TextView;<br>
      import android.widget.Toast;<br>
      import android.widget.ToggleButton;<br>
      public class Start extends Activity {<br>
          public WifiManager wifi;<br>
          boolean status;<br>
          Context context;<br>
          public TextView textStatus;<br>
          public LocationManager locationManager;<br>
          public LocationListener locationListener;<br>
          public BroadcastReceiver receiver;<br>
          public LocationProvider locationProvider;  </p>
    </blockquote>
  </blockquote>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {  
          try{   
          super.onCreate(savedInstanceState);           
          TextView textview = new TextView(this);  
          textview.setText("Toggle to On r Off the Application");  
          setContentView(textview);  
          ToggleButton btnonoff = new ToggleButton(this);  
          setContentView(btnonoff);  
          if(btnonoff.isChecked()){  
             status=true;  
              wifi.setWifiEnabled(true);  
              try{  
              // Setup WiFi  
              wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
              locationManager = (LocationManager)   getSystemService(Context.LOCATION_SERVICE);  
              if (receiver == null)  
                  receiver = new WiFiFilter(this);                
              registerReceiver(receiver, new IntentFilter(  
              WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));  
              if (locationListener ==null ){locationListener=new gpsLocation(this);} 
              }  
              catch(Exception e){Toast.makeText(context, WIFI_SERVICE, Toast.LENGTH_LONG).show();}  
         }   
         else if(!btnonoff.isChecked()) {  
            status=false;    
             if(wifi.isWifiEnabled()){  
             wifi.setWifiEnabled(false);  
             locationManager.removeUpdates((LocationListener) this);  
             }  
              }               
          }  
          catch(Exception e)  
          {  
              android.util.Log.i("PREFS",e.getMessage());  
          }  
  }     }  
  </code></pre>
  
  <blockquote>
    <p><strong>WifiFilter.java</strong>  </p>
    
    <blockquote>
      <p>package project.sow;<br>
      import java.util.List;<br>
      import android.content.BroadcastReceiver;<br>
      import android.content.Context;<br>
      import android.content.Intent;<br>
      import android.net.wifi.ScanResult;<br>
      import android.net.wifi.WifiManager;<br>
      import android.util.Log;<br>
      import android.widget.Toast;<br>
      public class WiFiFilter extends BroadcastReceiver {<br>
        private static final String TAG = "WiFiFilter";<br>
        Start start;<br>
          public WiFiFilter(Start start) {<br>
          super();<br>
          this.start=start;<br>
        }<br>
        //Preparing for Hidden SSID and saving the wifi config for later use<br>
              @Override<br>
        public void onReceive(Context c, Intent intent) {<br>
          List results = start.wifi.getScanResults();<br>
          List results2 = null;<br>
          ScanResult filter = null;<br>
          //filtering SSID<br>
          for(ScanResult filterresult : results ){<br>
             if(filterresult.SSID=="VOLSBB")<br>
             {<br>
                 results2 = results;}<br>
              }<br>
           //Checking the signal Level<br>
            for (ScanResult result : results2) {<br>
            if (filter == null<br>
                || WifiManager.compareSignalLevel(filter.level, result.level) &lt; 0)<br>
              filter = result;<br>
            try{<br>
            WifiManager.ACTION_PICK_WIFI_NETWORK.equals(filter);<br>
            }<br>
            catch(Exception e){String res="Exception:No network";Toast.makeText(start, res,   Toast.LENGTH_LONG).show();};<br>
          }<br>
          String message = String.format("Connected to S0W");<br>
          if(filter.SSID != null){<br>
          //Toast.makeText(start, message, Toast.LENGTH_LONG).show();<br>
          Log.d(TAG, "onReceive() message: " + message);<br>
        }<br>
         }  }</p>
    </blockquote>
  </blockquote>
  
  <p><strong>gpsLocation.java</strong>  <br></p>
  
  <blockquote>
    <p>package project.sow;<br>
    import project.sow.CustomWebView;<br>
    import android.location.Criteria;<br>
    import android.location.GpsStatus;<br>
    import android.location.Location;<br>
    import android.location.LocationListener;<br>
    import android.location.LocationManager;<br>
    import android.os.Bundle;<br>
    import project.sow.Start;<br>
    public class gpsLocation implements LocationListener{<br>
        Start sowWifigps;<br>
        public Location currentBestLocation;<br>
        public Location location;<br>
        GpsStatus status;<br>
        CustomWebView webviewgps;<br>
        double a;<br>
        double b;<br>
        @SuppressWarnings("unused")<br>
        private String bestProvider;<br>
        //public LocationManager locationManager;<br>
            public String provider1;<br>
        public gpsLocation(Start sowWifigps) {<br>
            super();<br>
            this.sowWifigps=sowWifigps;<br>
            // TODO Auto-generated constructor stub<br>
            Criteria criteria = new Criteria();<br>
            //bestProvider = sowWifigps.locationManager.getBestProvider(criteria, false);<br>
            try{<br>
            if(LocationManager.GPS_PROVIDER==null){<br>
                provider1 = LocationManager.NETWORK_PROVIDER;<br>
                }<br>
                else {provider1=LocationManager.GPS_PROVIDER;}<br>
            }catch( Exception e){ provider1=sowWifigps.locationManager.getBestProvider(criteria, false);}<br>
        }<br>
     @Override<br>
     public void onLocationChanged(Location location) {<br>
        // TODO Auto-generated method stub<br>
         sowWifigps.locationManager.requestLocationUpdates(provider1, 12000, 200, sowWifigps.locationListener);<br>
     }<br>
     @Override<br>
     public void onProviderDisabled(String provider) {<br>
        // TODO Auto-generated method stub<br>
         //provider = LocationManager.NETWORK_PROVIDER;<br>
         currentBestLocation=sowWifigps.locationManager.getLastKnownLocation(provider1);<br>
         a=currentBestLocation.getLatitude();<br>
            b=currentBestLocation.getLongitude();<br>
            webviewgps.loadUrl("<a href="http://localhost/mobile/default_mobile.php?lat=" rel="nofollow noreferrer">http://localhost/mobile/default_mobile.php?lat=</a>"+a+"&amp;long="+b); //change to site name<br>
     }<br>
     @Override<br>
     public void onProviderEnabled(String provider) {<br>
        // TODO Auto-generated method stub<br>
        sowWifigps.locationManager.requestLocationUpdates(provider1, 12000, 200, sowWifigps.locationListener);<br>
        a=currentBestLocation.getLatitude();<br>
        b=currentBestLocation.getLongitude();<br>
        webviewgps.loadUrl("<a href="http://localhost/mobile/default_mobile.php?lat=" rel="nofollow noreferrer">http://localhost/mobile/default_mobile.php?lat=</a>"+a+"&amp;long="+b); //change to site name<br>
     }<br>
     @Override<br>
     public void onStatusChanged(String provider, int status, Bundle extras) {<br>
        // TODO Auto-generated method stub<br>
                sowWifigps.locationListener.onLocationChanged(location);<br>
     }<br>
    }  </p>
  </blockquote>
  
  <p><strong>Am presenting here second Tab and other resembles this one.So am omitting it.</strong>    </p>
  
  <p><br><strong>Info.java</strong><br></p>
  
  <blockquote>
    <p>package project.sow;<br>
    import android.app.Activity;<br>
    import android.os.Bundle;<br>
    public class Info extends Activity {<br>
         public void onCreate(Bundle savedInstanceState) {<br>
                super.onCreate(savedInstanceState);<br>
            }<br>
         @Override<br>
            protected void onResume() {<br>
                CustomWebView webView = new CustomWebView(this);<br>
                webView.loadUrl("<a href="http://www.google.com/?s=Info" rel="nofollow noreferrer">http://www.google.com/?s=Info</a>");<br>
                setContentView(webView);<br>
                super.onResume();<br>
            }  }  </p>
  </blockquote>
  
  <p><br><br></p>
  
  <blockquote>
    <blockquote>
      <p><strong>CustomWebView.java</strong>    <br>
      package project.sow;<br>
      import android.app.ProgressDialog;<br>
      import android.content.Context;<br>
      import android.graphics.Bitmap;<br>
      import android.view.View;<br>
      import android.webkit.WebView;<br>
      import android.webkit.WebViewClient;<br>
      public class CustomWebView  extends WebView {<br>
          private ProgressDialog pgBar = null;<br>
          @SuppressWarnings("unused")<br>
          private Context ctx;<br>
          public CustomWebView(Context context) {<br>
              super(context);<br>
              this.clearCache(true);<br>
              this.clearFormData();<br>
              this.clearHistory();<br>
              this.getSettings().setJavaScriptEnabled(true);<br>
              this.getSettings().setUserAgentString(null);<br>
              this.requestFocus(View.FOCUS_DOWN);<br>
              this.setWebViewClient(new CustomWebView.CustomWebViewClient());<br>
              this.pgBar = new ProgressDialog(context);<br>
              this.pgBar.setMessage("Loading...");<br>
              ctx = context;<br>
          }<br>
          private class CustomWebViewClient extends WebViewClient {<br>
              @Override<br>
              public void onPageStarted(WebView view, String url, Bitmap favicon)   {<br>
                  super.onPageStarted(view, url, favicon);<br>
                  if (!CustomWebView.this.pgBar.isShowing()) {<br>
                      CustomWebView.this.pgBar.show();<br>
                  }<br>
              }<br>
              @Override<br>
              public void onPageFinished(WebView view, String url) {<br>
                  super.onPageFinished(view, url);<br>
                  if (CustomWebView.this.pgBar.isShowing()) {<br>
                      CustomWebView.this.pgBar.dismiss();<br>
                  }<br>
              }<br>
              @Override<br>
              public boolean shouldOverrideUrlLoading(WebView view, String url) {<br>
                  try{<br>
                      view.loadUrl(url);<br>
                  }catch(Exception ex){<br>
                  }<br>
                  return true;<br>
              }  }  }</p>
    </blockquote>
  </blockquote>
  
  <p>main.XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;  
  &lt;TabHost android:layout_width="fill_parent"  
      android:layout_height="fill_parent" xmlns:android="http://schemas.android.com  /apk/res/android" android:id="@android:id/tabhost" android:background="#cabfa9"&gt;  
      &lt;LinearLayout android:id="@+id/LinearLayout01"  
          android:orientation="vertical" android:layout_height="fill_parent"  
          android:layout_width="fill_parent"&gt;  
  &lt;TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content"   android:layout_width="fill_parent"   android:background="#1b3b5b" &gt;&lt;/TabWidget&gt;  
          &lt;FrameLayout android:id="@android:id/tabcontent"   android:layout_height="fill_parent" android:layout_width="fill_parent"   android:scrollbars="horizontal" android:background="#cabfa9"&gt;           
      &lt;/FrameLayout&gt;  
      &lt;/LinearLayout&gt;  
  &lt;/TabHost&gt;
  </code></pre>
  
  <p><strong>style.xml</strong> </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;style name="tabText" parent="@android:style/Theme.Black"&gt;        
          &lt;item name="android:textColor"&gt;#FFFFFF&lt;/item&gt;
          &lt;item name="android:textSize"&gt;10dip&lt;/item&gt;        
      &lt;/style&gt;    
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>`</p>
  
  <p><strong>ic_sample</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;!-- When selected, use grey --&gt;
      &lt;item android:drawable="@drawable/icon1"
            android:state_selected="true" /&gt;
      &lt;!-- When not selected, use white--&gt;
      &lt;item android:drawable="@drawable/icon2" /&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <blockquote>
    <p>I have added the following permissions  in the manifest file</p>
  </blockquote>
  
  <p><code>&lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;</code></p>