Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

android - How to launch a PopupWindow or Dialog from an input method service?

I get the same exception when I try to pop a PopupWindow (or Dialog) from 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 pop a Dialog instead, I get the exact same exception in the exact same line of ViewRoot.java. Here is my code (abridged):

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

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'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 Service (specifically an InputMethodService), as opposed to an Activity?

Thanks in advance,

Barry

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually i managed to do it try this:

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                 builder.setTitle("Make your selection");
                 builder.setItems(items, new DialogInterface.OnClickListener()
                 {
                    public void onClick(DialogInterface dialog, int item)
                    {
                       // Do something with the selection
                    }
                 });
                 AlertDialog alert = builder.create();
                 Window window = alert.getWindow();
                 WindowManager.LayoutParams lp = window.getAttributes();
                 lp.token = mInputView.getWindowToken();
                 lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
                 window.setAttributes(lp);
                 window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                 alert.show();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...