title: WindowManager$BadTokenException: Unable to add window -- token in popup window
body: <p>I made custom popup dialog based on the <a href="http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/" rel="nofollow noreferrer">following link</a> here my dialog. 
  <img src="https://i.stack.imgur.com/3a2bN.png" alt="enter image description here"></p>
  
  <p>I have button list to show the popup. When I click these button, this dialog box will be shown. My problem is a little strange. Sometimes When I select the note edit text in dialog, I got the WindowManager$BadTokenException. </p>
  
  <pre><code>06-06 12:34:57.878: E/AndroidRuntime(22951): FATAL EXCEPTION: main
  06-06 12:34:57.878: E/AndroidRuntime(22951): android.view.WindowManager$BadTokenException: Unable to add window --
  token android.view.ViewRootImpl$W@417d8d98 is not valid; is your activity running?
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.Window$LocalWindowManager.addView(Window.java:547)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:845)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor$PinnedPopupWindow.updatePosition(Editor.java:2147)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor$PinnedPopupWindow.show(Editor.java:2104)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor$SuggestionsPopupWindow.show(Editor.java:2349)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor.showSuggestions(Editor.java:1647)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor$1.run(Editor.java:1546)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.os.Handler.handleCallback(Handler.java:615)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.os.Handler.dispatchMessage(Handler.java:92)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.os.Looper.loop(Looper.java:137)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at android.app.ActivityThread.main(ActivityThread.java:4745)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at java.lang.reflect.Method.invokeNative(Native Method)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at java.lang.reflect.Method.invoke(Method.java:511)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  06-06 12:34:57.878: E/AndroidRuntime(22951): at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I don't know why? I didn't much change the coding. I added <code>mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)</code> in the <code>popupwindow</code> constructor to show the dialog above the keyboard. If I run on the emulator, there is no problem. In the real device, I got exception. Is there any idea?</p>
  
  <p>In the button listener I implement like this </p>
  
  <pre><code> if(dialogNote == null) {
    dialogNote = new QuickActionNote(AddNewNoteActivity.this);
   }
   dialogNote.show(btn);
  </code></pre>
  
  <p>Thanks.</p>