转自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. [翻译svg教程]svg 中的g元素

    svg 中的<g>元素用来组织svg元素.如果一组svg元素被g元素包裹了,你可以通过对g元素进行变换(transform),被g元素包裹的元素也将被变换,就好这些被svg包裹的元素是一个 ...

  2. Linux下搭建coreseek(sphinx+mmseg3)全文检索

    测试平台:Center OS 1.设置环境,升级/安装系统基础依赖包:m4.autoconf.automake.libtool #设置路径和中文环境: $ export PATH=/usr/local ...

  3. View 与 Controller 之间的delegate(代理)传值

    这个代理传值是经常使用的一种传值方式,下面介绍一种View 和 Controller 之间的代理传值方法. 先建立一个View视图 如 LoginView 是继承于一个UIView 在LoginVie ...

  4. MSSQL数据库表加锁

    所指定的表级锁定提示有如下几种: 1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁. 2. NOLOCK:不添加共享锁和排它锁,当这个选项生效后,可 ...

  5. Java调用批处理或可执行文件

    import java.io.BufferedReader; import java.io.InputStreamReader; public class Test { public static v ...

  6. form data和request payload的区别

    HTML <form> 标签的 enctype 属性 在下面的例子中,表单数据会在未编码的情况下进行发送: <form action="form_action.asp&qu ...

  7. System.Dynamic.ExpandoObject 类型的简单使用

    该类型可以实现的是动态添加属性和移除属性,有点类似 js 中对象的操作,非常灵活 static void Main(string[] args) { dynamic obj = new System. ...

  8. eclipse插件开发入门

    2016-09-09 17:11:50 1. 概述 1.1 SWT/JFace 是Eclipse 的基础,Eclipse 的 Workbench 就是建立在 SWT/JFace 之上的.另外,JFac ...

  9. gulp es7配置文件

    http://sanwen.net/a/ybsfcoo.html /** * Created by udi on 2016/11/24. */ var gulp = require('gulp'); ...

  10. java中文乱码解决之道(二)-----字符编码详解:基础知识 + ASCII + GB**

    在上篇博文(java中文乱码解决之道(一)-----认识字符集)中,LZ简单介绍了主流的字符编码,对各种编码都是点到为止,以下LZ将详细阐述字符集.字符编码等基础知识和ASCII.GB的详情. 一.基 ...