title: "WindowManager BadTokenException-Unable to add window"Why i am getting this exception while using google maps?
body: <p>I am using google map to show(pin) different places.After showing different palces when hit the back button it redirects to my home screen.Around 10-15 seconds later my application gets closed.I believe that the location updates is still running in the background which causes this error.How to stop updating the activity in the background?</p>
  
  <pre><code>    package com.bar.start;
  
      import java.util.ArrayList;
      import org.apache.http.HttpEntity;
      import org.apache.http.conn.ClientConnectionManager;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
      import org.apache.http.params.HttpParams;
      import com.bar.barapp.R;
      import com.bar.location.Loc;
      import com.bar.location.Webcall;
      import com.google.android.gms.maps.CameraUpdateFactory;
      import com.google.android.gms.maps.GoogleMap;
      import com.google.android.gms.maps.MapFragment;
      import com.google.android.gms.maps.model.BitmapDescriptorFactory;
      import com.google.android.gms.maps.model.Circle;
      import com.google.android.gms.maps.model.LatLng;
      import com.google.android.gms.maps.model.MarkerOptions;
      import android.app.Activity;
      import android.app.ProgressDialog;
      import android.content.Context;
      import android.content.SharedPreferences;
      import android.location.Location;
      import android.location.LocationListener;
      import android.location.LocationManager;
      import android.os.AsyncTask;
      import android.os.Bundle;
      import android.print.PrintAttributes;
      import android.util.Log;
      import android.widget.ArrayAdapter;
  
      public class LocationActivity extends Activity implements LocationListener {
  
       private final String TAG = getClass().getSimpleName();
       protected ConnectionDetector connectionDetector;
       public Boolean isInternetPresent;
       private String[] places;
       public static Loc [] getloc;
       private LocationManager locationManager;
       private Location loc;
  
       static HttpEntity entityResponse = null;
       GoogleMap map;
       MarkerOptions mp;
       Activity activity;
       Context context;
       LocationListener listener;
       Double lat,lon;
      final   String Googlekey="APikey" ;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
  
  
          SharedPreferences sharedPreferences;
          int locationCount = 0;
          connectionDetector = new ConnectionDetector(getApplicationContext());
          isInternetPresent = connectionDetector.isConnectingToInternet();
      super.onCreate(savedInstanceState);
      setContentView(R.layout.location);
  
       LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
  
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
  
      }
  
      @Override
      public void onLocationChanged(Location location) {
  
          map.clear();
          lat=location.getLatitude();
          lon=location.getLongitude();
  
          Googleapicall exeTask = new Googleapicall();
            exeTask.execute();
  
            mp = new MarkerOptions();
  
             mp.position(new LatLng(lat, lon));
             map.addMarker(mp);
             mp.title("My Location");
     p.animateCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(lat, lon), 13));
  
      }
  
      @Override
      public void onProviderDisabled(String provider) {
  
  
      }
  
      @Override
      public void onProviderEnabled(String provider) {
  
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
  
      }
  
  
  
      public class Googleapicall extends AsyncTask&lt;String, Void, String&gt;
  
      {
  
  
          Loc statStr = null;
           Loc[] statInfoResult = null;
           ArrayList&lt;Loc&gt; SIArrayList = new ArrayList&lt;Loc&gt;();
  
          private ProgressDialog Dialog;
  
          @Override
          protected void onPreExecute() {
              Dialog = new ProgressDialog(LocationActivity.this);
              Dialog.setMessage(LocationActivity.this.getResources().getString(
                      R.string.loading));
              Dialog.setCancelable(false);
              Dialog.show();
  
          }
  
          @Override
          protected String doInBackground(String... params) {
  
              String result = "";
              try 
              {
  
                  if(isInternetPresent)
  
                  {
                  getloc=Webcall.getdet(lat,lon,Googlekey);
                  result = "Success";
                  System.out.println("getloc "+getloc.length);
  
                  }
  
  
                  else 
                  {
                      result = "Failure";
                  }
              } 
  
  
              catch (Exception e)
              {
                  result = "Failure";
                  e.printStackTrace();
              }
  
              return result;
  
  
          }
  
          @Override
          protected void onPostExecute(String result) {
  
  
              Loc lc=new Loc();
  
              try {
  
                  if(result.contains("Success"))
                  {
  
                      mp=new MarkerOptions();
  
                      for(int i=0;i&lt;getloc.length;i++)
                      {
                          Double lati=getloc[i].latitudes;
                          Double longi=getloc[i].longitudes;
                             map.addMarker(new MarkerOptions()
                                .title("Nearbyplace")
                                .position(
                                  new LatLng(lati,longi))
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.small)));
                      }
                  }
  
                  } 
              catch 
              (Exception e) 
              {
                  e.printStackTrace();    
  
              }
  
              Dialog.dismiss();
  
          }
  
      }
  
  
  }
  
  
  04-06 11:09:53.768: E/AndroidRuntime(5188):     FATAL EXCEPTION: main
  04-06 11:09:53.768: E/AndroidRuntime(5188):     android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@42ea1380 is not valid; is your activity running?
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:796)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.app.Dialog.show(Dialog.java:287)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.bar.start.LocationActivity$Googleapicall.onPreExecute(LocationActivity.java:194)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.AsyncTask.execute(AsyncTask.java:534)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.bar.start.LocationActivity.onLocationChanged(LocationActivity.java:104)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.Handler.dispatchMessage(Handler.java:99)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.Looper.loop(Looper.java:137)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.app.ActivityThread.main(ActivityThread.java:5419)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at java.lang.reflect.Method.invokeNative(Native Method)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at java.lang.reflect.Method.invoke(Method.java:525)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
  04-06 11:09:53.768: E/AndroidRuntime(5188):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>