title: Getting 'BadTokenException: Unable to add window' when trying to show Facebook dialog - Android
body: <p>I'm getting a Bad Token Exception when trying to show the publish on wall dialog from the Facebook SDK(It happens every 2 times I run the app).</p>
  
  <p>I have a 'publish' button, and its job is to show the dialog if the user is logged in to his FB account, or show the login dialog(and then immediately show the 'post on wall' dialog) if the user isn't logged in to his account.</p>
  
  <p>Here's the on click listener of the Publish button - </p>
  
  <pre><code>    mPostButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
              if(mLoginButton.getVisibility() == 0)
                  postOnWall();
              else
                  mLoginButton.performClick();
          }
      });  
  </code></pre>
  
  <p>Here's the onAuthSucceed() listener - </p>
  
  <pre><code>public void onAuthSucceed() {
      postOnWall();
  }
  </code></pre>
  
  <p>Here's the PostOnWall function(which shows the publish dialog) - </p>
  
  <pre><code> public void postOnWall()
   {
      Bundle params = new Bundle();
      params.putString("name", FBname);
      params.putString("link", FBlink);
      params.putString("description", FBdescription);
      params.putString("picture", FBpicture);
      con = this;
      mFacebook.dialog(con, "feed", params, new SampleDialogListener());
   }
  </code></pre>
  
  <p>Here's the log of the error - </p>
  
  <pre><code> 05-05 16:25:09.601: WARN/WindowManager(109): Attempted to add application window with unknown token HistoryRecord{405416b0 android.alco/.do_drive}.  Aborting.
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405db9f8 is not valid; is your activity running?
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.view.ViewRoot.setView(ViewRoot.java:527)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.app.Dialog.show(Dialog.java:241)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at com.facebook.android.Facebook.dialog(Facebook.java:622)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.alco.do_drive.postOnWall(do_drive.java:258)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.alco.do_drive$SampleAuthListener.onAuthSucceed(do_drive.java:172)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.alco.SessionEvents.onLoginSuccess(SessionEvents.java:78)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at android.alco.LoginButton$LoginDialogListener.onComplete(LoginButton.java:100)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at com.facebook.android.Facebook$1.onComplete(Facebook.java:308)
   05-05 16:25:27.292: ERROR/AndroidRuntime(20089):     at com.facebook.android.FbDialog$FbWebViewClient.shouldOverrideUrlLoading(FbDialog.java:133)
  </code></pre>
  
  <p>I've looked all over and I can't find a solution.</p>
  
  <p>EDIT - This only happens when the user is not logged in. In that case, he presses the 'publish' button, logs in, and then the 'postOnWall' function is called, which generates the error. I'm working on it for 2 days now, and couldn't find anything about it.</p>
  
  <p>Thanks!</p>