转自http://www.cnblogs.com/oakpip/archive/2011/04/06/2007310.html

错误产生:

  1. private Context mcontext;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();
  4. System.out.println("mcontext=" + mcontext);
  5. }
  1. new AlertDialog.Builder(mcontext)
  2. .setIcon(android.R.drawable.ic_dialog_alert)
  3. .setTitle("Warnning")
  4. .setMessage(
  5. "You forget to write the message. Do you want to fill out it ??")
  6. .setPositiveButton("Yes", positiveListener).setNegativeButton(
  7. "No", negativeListener).create().show();

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

解决方法:

将new AlertDialog.Builder(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。

  1. new AlertDialog.Builder(MyActivity.this)
  2. .setIcon(android.R.drawable.ic_dialog_alert)
  3. .setTitle("Warnning")
  4. .setMessage(
  5. "You forget to write the message. Do you want to fill out it ??")
  6. .setPositiveButton("Yes", positiveListener).setNegativeButton(
  7. "No", negativeListener).create().show();

Android自用-----WindowManager$BadTokenException: Unable to add window -- token null is not for an application的更多相关文章

  1. android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

      原博客地址:http://aijiawang-126-com.javaeye.com/blog/662336 在Activity中newSpinner是我把mContext传入,但是出了 andr ...

  2. Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

    在广播中启动一个Dialog时出现如下错误信息:Caused by: android.view.WindowManager$BadTokenException: Unable to add windo ...

  3. android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic

    之前遇到过这样的问题, 04-12 10:40:33.302: E/AndroidRuntime(17213): Caused by: android.view.WindowManager$BadTo ...

  4. Activity has leaked window that was originally added -界面退出时未关闭对话框异常 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? -

    退出Activity时弹出登录框,点击确定finish当前Activity,结果报了这个错,随后查找资料知道 原因: 是因为退出Activity时没有关闭弹出框,出现了这个错误 解决方法: 只需要在a ...

  5. Unable to add window -- token null is not for an application错误的解决方法 android开发

    Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not f ...

  6. bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token

    ========4       关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...

  7. Unable to add window -- token null is not for an application

    导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApp ...

  8. 自定义对话框 提示:Unable to add window token null is not for an application

    这是因为在new Dialog(context);的时候传入的context是通过getApplicationContext()获得的,这样就会报错. 把context的获得方式改为MainActiv ...

  9. 关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window

    下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token ...

随机推荐

  1. C语言-占位符

     %a                浮点数.十六进制数字和p-记数法(C99) %A 浮点数.十六进制数字和p-记法(C99) %c 一个字符 %d 有符号十进制整数 %e 浮点数.e-记数法 %E ...

  2. [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)

    再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...

  3. CSS列表逆序

    要使列表逆序的话,大多数人包括我一半都会选择在ol标签里使用reversed属性 <ol reversed> <li>first</li> <li>se ...

  4. HTML5

    一.头部设置 <!--页面窗口自动调整到设备宽度,并禁止用户及缩放页面--> <meta name="viewport" content="width= ...

  5. keepalived 原主上线时vip漂移情况

    1. 设置为MASTER,BACKUP 优先级相同 的情况: master端的keepalived起来就会获取到vip变成主. 2. 设置为BACKUP,BACKUP 一个优先级高一个优先级低 的情况 ...

  6. hexo建个人博客

    已经在腾讯云获得了域名和服务器,想着既然已经这样了,就折腾折腾自己的个人博客主页吧. 考虑再三决定用github pages来实现我的博客.github Pages可以被认为是用户编写的.托管在git ...

  7. 编译链接 C++

    预处理之后的源文件被称为一个编译单位,也即编译器的工作对象.为了使编译能够进行,程序员必须提供各种程序其他部分的声明来孤立分析一个编译单位.所有名字空间,类,函数都应该在他们所在的编译单位中有声明,所 ...

  8. Window.focus()让页面成为当前窗体

    Window.focus()让页面成为当前窗体 最近在弄在线客服的时候,想在收到信息时候让窗体自动弹出到最前,最小化的时候也是弹出到最前.本来以为很麻烦,问了好多人,都不知道,在网上查资料也没有查到. ...

  9. DOMO1

    以下是Demo首页的预览图 demo下载:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NjE0Njh8ZTIyZDA2M2N8 ...

  10. FFT时域与频域的关系,以及采样速率与采样点的影响

    首先对于FFT来说,输入的信号是一个按一定采样频率获得的信号序列,而输出是每个采样点对应的频率的幅度(能量). 下面详细分析: 在FFT的输出数据中,第一个值是直流分量的振幅(这样对应周期有无穷的可能 ...