I get the same exception when trying to popup a PopupWindow (or Dialog) from an InputMethodService:
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) ...
If I try to use the dialog instead, I get the same exception on the same ViewRoot.java line. Here is my code (abbreviated):
public class MyInputMethodService extends InputMethodService implements View.OnClickListener { public void onClick(View v) {
and
<?xml version="1.0" encoding="utf-8"?> <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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="Test Pop-Up" /> </LinearLayout>
I tried many variations of the above code, but always get the same exception for PopupWindows and Dialogs. For some reason, Toast warnings are triggered. Is there a special way to launch PopupWindow or Dialog from a Service (in particular, InputMethodService), and not Activity?
Thanks in advance,
Barry
android android widget
Barry Fruitman Apr 18 '11 at 5:00 a.m. 2011-04-18 05:00
source share