Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to launch a PopupWindow or Dialog from an input method service?
    primarykey
    data
    text
    <p>I get the same exception when I try to pop a PopupWindow (or Dialog) from InputMethodService:</p> <pre><code>FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.view.ViewRoot.setView(ViewRoot.java:505) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) at android.widget.PopupWindow.invokePopup(PopupWindow.java:828) at android.widget.PopupWindow.showAtLocation(PopupWindow.java:688) at mypackage.MyInputMethodService.onClick(MyInputMethodService.java:123) ... </code></pre> <p>If I try to pop a Dialog instead, I get the exact same exception in the exact same line of ViewRoot.java. Here is my code (abridged):</p> <pre><code>public class MyInputMethodService extends InputMethodService implements View.OnClickListener { public void onClick(View v) { // This is the handler for View.OnClickListener LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false), 100, 100, true); pw.showAtLocation(mInputView, Gravity.CENTER, 0, 0); // mInputView was previously created and returned by onCreateInputView() } } // end of MyInputMethodService </code></pre> <p>and</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="Test Pop-Up" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>I've tried many variations of the above code but always get the same exception for PopupWindows and Dialogs. For some reason Toast alerts work. Is there a special technique for launching a PopupWindow or Dialog from a <strong>Service</strong> (specifically an InputMethodService), as opposed to an Activity?</p> <p>Thanks in advance,</p> <p>Barry</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload