问题描述
我,当我尝试弹出一个PopupWindow(或Dialog)的InputMethodService得到相同的异常:
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)
...
如果我试图弹出一个对话框,而不是,我得到ViewRoot.java完全相同的路线完全相同的例外。这是我的code(有删节):
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
和
<?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>
我已经试过了上述code许多变化,但总是得到相同的例外PopupWindows和对话框。出于某种原因,吐司警报工作。是否有启动从服务一个PopupWindow或Dialog的特殊技术(特别是InputMethodService),而不是一个活动?
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?
在此先感谢,
巴里
推荐答案
其实我设法做到这一点试试这个:
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();
这篇关于如何启动从一个输入法服务PopupWindow或对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!