onCreate方法中调用PopupWindow的错误:android.view.WindowManager$BadTo

文章详细介绍了在Android Activity的OnCreate方法中使用PopupWindow时遇到的'android.view.WindowManager$BadTokenException'错误,并提供了解决方案。通过将PopupWindow的显示代码移出OnCreate方法,在Activity加载完毕后进行显示,避免了窗口令牌无效的问题。文中还分享了一个巧妙的方法,即在Activity获得焦点后触发PopupWindow的显示,确保了Activity已经完全加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如题:在activity的oncreate方法中使用popupwindow出现以下错误:
android.view.WindowManager$BadTokenException: Unable to add window --
token null is not valid; is your activity running?

错误代码如下 :

1
2
pop = new PopupWindow(pop_view,320,250);
pop.showAtLocation(parent, Gravity.TOP,0, 0);

解决方法:
应把pop.showAtLocation(parent, Gravity.TOP,0, 0)这一句移出oncreate方法,在控件渲染完毕后再使用。

但是移出oncreate方法的话该移到哪里去呢?网友的方法大概是这几种:

1、移到事件中(比如一个button的click事件中);
2、移到子线程中;另起一线程,在线程中不断循环,直到判断控件是否渲染完毕(如长宽大于0),不推荐。。。
3、移到重写的控件(parent)中,在控件ondraw()完后生成pop。
ps:1、2绝对没问题,3没测试过。


后来在网上找到一个绝佳的方法:

1
2
3
4
5
6
7
8
@Override
public void onWindowFocusChanged(boolean hasFocus) {
  // TODO Auto-generated method stub
  super .onWindowFocusChanged(hasFocus);
  if (hasFocus){
   showPopupWindow(getApplicationContext());
  }
}

其中showPopupWindow(getApplicationContext())是我自己定义的专门显示popupwindow的一个函数。

当activity获得焦点之后,activity是加载完毕的了,这个方法的技巧性比较强,很难想到。

具体可以参考这篇文章。


转自: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0304/963.html

07-20 19:57:40.672 5131 5131 D AndroidRuntime: Shutting down VM --------- beginning of crash 07-20 19:57:40.673 5131 5131 E AndroidRuntime: FATAL EXCEPTION: main 07-20 19:57:40.673 5131 5131 E AndroidRuntime: Process: com.adayo.aaop_deviceservice, PID: 5131 07-20 19:57:40.673 5131 5131 E AndroidRuntime: java.lang.RuntimeException: Unable to create service com.adayo.aaop_deviceservice.service.BurialPointService: android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@e6477e5 -- the specified display can not be found 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:4221) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread.access$1600(ActivityThread.java:238) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.os.Looper.loop(Looper.java:223) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7731) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: Caused by: android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@e6477e5 -- the specified display can not be found 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:1099) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.Dialog.show(Dialog.java:342) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.Presentation.show(Presentation.java:257) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.adayo.aaop_deviceservice.service.BurialPointService.x(Unknown Source:77) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.adayo.aaop_deviceservice.service.BurialPointService.A(Unknown Source:50) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.adayo.aaop_deviceservice.service.BurialPointService.onCreate(Unknown Source:10) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:4209) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: ... 8 more 07-20 19:57:40.676 5131 5131 I Process : Sending signal. PID: 5131 SIG: 9 private void createBackgroundToQnx() { DisplayManager displayManager = (DisplayManager)this.getSystemService(Context.DISPLAY_SERVICE); Display[] displays = displayManager.getDisplays(); Log.i(TAG, "createBackgroundToQnx displays = " + Arrays.toString(displays)); for (Display display : displayManager.getDisplays()) { if (display.getDisplayId() == 1) { Presentation presentation = new Presentation(this, displayManager.getDisplay(1)); View view = new View(this); view.setBackgroundColor(Color.BLACK); presentation.setContentView(view); presentation.show(); } } }
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值