如何从输入法服务启动PopupWindow或Dialog?
|
当我尝试从InputMethodService弹出PopupWindow(或Dialog)时,出现相同的异常:
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的完全相同的行中得到完全相同的异常。这是我的代码(节略):
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>
我已经尝试了上述代码的许多变体,但对于PopupWindows和Dialogs总是会遇到相同的异常。由于某些原因,Toast警报起作用。是否有一种特殊的技术可以从服务(特别是InputMethodService)(而不是Activity)启动PopupWindow或Dialog?
提前致谢,
巴里
没有找到相关结果
已邀请:
1 个回复
很缴