关于android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an app

本文解决了一个在Android Activity中使用newAlertDialog.Builder(Context context)创建Dialog时报出的android.view.WindowManager$BadTokenException错误。通过将参数替换为Activity.this,成功解决了问题并创建了正确的Dialog。
原博客地址:http://aijiawang-126-com.javaeye.com/blog/662336
在Activity中newSpinner是我把mContext传入,但是出了android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application这个错误,参考了达人的文章终于搞定。
  1. private Context mcontext;
  2.         @Override
  3.         protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();
  4.                 System.out.println("mcontext=" + mcontext);
  5.         }[code]        new AlertDialog.Builder(mcontext)
  6.                                 .setIcon(android.R.drawable.ic_dialog_alert)
  7.                                 .setTitle("Warnning")
  8.                                 .setMessage(
  9.                                                 "You forget to write the message. Do you want to fill out it ??")
  10.                                 .setPositiveButton("Yes", positiveListener).setNegativeButton(
  11.                                                 "No", negativeListener).create().show();
复制代码
[/code]


导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。 



解决方法:将new AlertDialog.Builder(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。
01-01 00:16:20.586 20968 20968 E AndroidRuntime: FATAL EXCEPTION: main 01-01 00:16:20.586 20968 20968 E AndroidRuntime: Process: com.adayo.service.atsmode, PID: 20968 01-01 00:16:20.586 20968 20968 E AndroidRuntime: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:1071) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.app.Dialog.show(Dialog.java:340) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.app.AlertDialog$Builder.show(AlertDialog.java:1131) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.widget.VideoView$5.onError(VideoView.java:615) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:3446) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.os.Looper.loop(Looper.java:223) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7687) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 01-01 00:16:20.586 20968 20968 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 01-01 00:16:21.454 20968 20968 I Process : Sending signal. PID: 20968 SIG: 9
08-06
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
### 解决 Android 视频播放时出现 `android.view.WindowManager$BadTokenException` 异常 当应用程序尝试显示对话框或其他窗口组件时,如果活动(Activity)已经结束或尚未启动,则会抛出 `android.view.WindowManager$BadTokenException` 异常。对于视频播放场景中的此异常,通常是因为在不合适的生命周期阶段调用了与UI交互的方法。 #### 确认 Activity 生命周期状态 确保所有的 UI 更新操作都在有效的 Activity 生命周期内执行。例如,在创建自定义 Dialog 或者 VideoView 的时候,应该确认当前的 Activity 是处于可交互的状态[^1]。 ```java if (!isFinishing()) { // 安全地弹出自定义Dialog或者其他视图更新逻辑 } ``` #### 防止异步任务影响 如果有后台线程正在处理数据并打算通过 Handler 发送消息来刷新界面,那么需要特别注意这些消息是否会在 Activity 销毁之后才到达。可以考虑取消所有待定的消息或者移除回调函数以防止它们触发可能导致该异常的操作[^2]。 ```java @Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacksAndMessages(null); // 清理Handler中剩余的任务 } ``` #### 使用 Fragment 对话框替代传统 Dialog Fragment 提供了一种更安全的方式来管理临时性的用户输入控件。相比于直接实例化 AlertDialog 并立即展示它的方式,采用 DialogFragment 可以更好地配合支持库以及不同版本间的兼容性问题[^3]。 ```java public static class MyVideoPlayerDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // 构建和返回你的视频播放器dialog } // ...其他必要的方法... } ``` #### 检查权限配置 某些情况下,缺少特定的应用程序权限也可能引发类似的错误。虽然这不是最常见的原因,但在排查过程中还是值得检查一下 manifest 文件里是否有声明访问网络摄像头等硬件资源所需的权限[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值