Android输入法window类型,android – 如何从输入法服务启动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)

...

如果我尝试弹出一个Dialog,我会在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

android:orientation="vertical"

android:padding="10dip"

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dip"

android:text="Test Pop-Up"

/>

我已经尝试了上述代码的许多变体,但总是得到PopupWindows和Dialogs的相同异常.出于某种原因Toast警报工作.是否有一种特殊的技术可以从服务(特别是InputMethodService)启动PopupWindow或Dialog,而不是Activity?

提前致谢,

巴里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值