title: android.view.WindowManager$BadTokenException: Unable to add window --
body: <p>We are running a long running application (runs for over 5 hrs) to track the path through the GPS. 
  We are not growing any data over time. 
  Using a few global variable to maintain distance and time. 
  But when we are trying to show the Dialog box some time we are getting crash, but very hard to reproduce.
  This is the stack trace</p>
  
  <pre><code>    android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@406ea4e0 is not valid; is your activity running?
  at android.view.ViewRoot.setView(ViewRoot.java:532)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  at android.view.Window$LocalWindowManager.addView(Window.java:424)
  at android.app.Dialog.show(Dialog.java:241)
  at eventHandlers.SavariMapActivityEventHandler.HandleEndTrip(SavariMapActivityEventHandler.java:104)
  at eventHandlers.SavariMapActivityEventHandler.onClick(SavariMapActivityEventHandler.java:50)
  at android.view.View.performClick(View.java:2485)
  at android.view.View$PerformClick.run(View.java:9080)
  at android.os.Handler.handleCallback(Handler.java:587)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:130)
  at android.app.ActivityThread.main(ActivityThread.java:3687)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:507)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I saw these posts</p>
  
  <p><a href="http://android.rwmthings.com/issues/android-view-windowmanagerbadtokenexception/" rel="nofollow">Link 1</a>
  <a href="http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/" rel="nofollow">Link 2</a></p>
  
  <p>and many other solution every one is telling to check activity.isFinishing(), and even i didn't use getApplicationContext() to show the dialog. 
  when we didn't called finish().very strange this is happning. Please let me know your thoughts.</p>
  
  <p>Java code for displaying the dialogbox which is on click of button:</p>
  
  <p>HandleEndTrip() method which is called on click of button</p>
  
  <p>For all eventhandeler regarding the activity i created a java class </p>
  
  <pre><code>public class SavariMapActivityEventHandler extends EventHandler {
      private static SavariMapActivityEventHandler instance = null;
  
      @Override
      public void handleClick(Activity activity) {
          setActivity(activity);
          INITClick();
      }
  
      @Override
      public void INITClick() {
          getActivity().findViewById(R.id.endtrip).setOnClickListener(this);
          getActivity().findViewById(R.id.expensebutton).setOnClickListener(this);
          getActivity().findViewById(R.id.details).setOnClickListener(this);
  
      }
  
      private SavariMapActivityEventHandler() {
  
  
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()) {
          case R.id.endtrip:
              HandleEndTrip();
                break;
  
          }
      }
  
      private void HandleEndTrip() {
          AlertDialog alertDialog = new Builder(getActivity()).create();
          alertDialog.setTitle("Confirmation");
          alertDialog.setMessage("Has the trip ended?");
          alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface arg0, int arg1) {
                  arg0.dismiss();
              }
          });
          alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface arg0, int arg1) {
                  try {
                      getActivity().endthread = true;
                      getActivity().getHandler().removeCallbacksAndMessages(null);
                      TimeCalculator calculator = new TimeCalculator(
                              getActivity(), System.currentTimeMillis(), holder
                                      .getStartfromGarageTime());
                      CustomerInfo.getInstance().setEndduration(
                              calculator.getHour());
                      getActivity().finish();
                      SavariMapActivityEventHandler.getInstance()
                              .finishActivity();
                      getActivity().startActivity(
                              new Intent(getActivity(),
                                      BillingConfirmActivity.class));
  
                  }
                  catch (Exception e)
                  {
                      SavariUserSettings.appendLog("Savaari MapAcivityEventHandeler", ""+e+" "+e.getStackTrace()[0].getLineNumber());                 
                  }
              }
          });
  
          alertDialog.show();
  
      }
  
      @Override
      public SavariMapActivity getActivity() {
          return (SavariMapActivity) super.getActivity();
      }
  
      public static SavariMapActivityEventHandler getInstance()
      {
          if (instance == null)
              instance = new SavariMapActivityEventHandler();
          return instance;
      }
  
      public static void setInstance(SavariMapActivityEventHandler instance) {
          SavariMapActivityEventHandler.instance = instance;
      }
  }
  </code></pre>
  
  <p>Code for EventHandler:</p>
  
  <pre><code>    public abstract class EventHandler implements OnClickListener {
  private ProgressDialog dialog;
  public final String SETTINGS_PREFERENCE = "setting_pref";
  private Activity activity;
  
  public abstract void handleClick(Activity activity);
  
  public abstract void INITClick();
  
  public ValueHolder holder = null;
  
  protected final void setActivity(Activity activity) {
      this.activity = activity;
      holder = new ValueHolder(activity);
  
  }
  
  public final void finishActivity() {
      activity.finish();
  }
  
  public final void INITDialog(String title, String mesg) {
      dialog = new ProgressDialog(activity);
      dialog.setTitle(title);
      dialog.setMessage(mesg);
      dialog.show();
  }
  
  public final void finishDialog() {
      try {
          if (dialog != null)
              dialog.dismiss();
          } catch (Exception e)
          {
  
          }
  }
  
  public Activity getActivity() {
      return activity;
  }
  </code></pre>
  
  <p>}</p>