原博客地址: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. }
  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.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Android自用-----WindowManager$BadTokenException: Unable to add window -- token null is not for an application

    转自http://www.cnblogs.com/oakpip/archive/2011/04/06/2007310.html 错误产生: private Context mcontext; @Ove ...

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

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

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

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

  7. 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 ...

  8. android.view.WindowManager$BadTokenException: Unable to add window

    这是在加载dialog时出现的一个异常.转载地址:http://hi.baidu.com/fbdfp/item/7dea2d0ade9121813d42e23d 扔了好久的android又开始断断续续 ...

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

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

随机推荐

  1. java NIO与IO的区别

    nio是new io的简称,从jdk1.4就被引入了.现在的jdk已经到了1.6了,可以说不是什么新东西了.但其中的一些思想值得我来研究.这两天,我研究了下其中的套接字部分,有一些心得,在此分享. 首 ...

  2. 3563: DZY Loves Chinese - BZOJ

    Description神校XJ之学霸兮,Dzy皇考曰JC.摄提贞于孟陬兮,惟庚寅Dzy以降.纷Dzy既有此内美兮,又重之以修能.遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上有N座祭 ...

  3. linux进程管理之开机启动

    下面用自启动apache为例;自启动脚本:/usr/local/apache2/bin:./apachectl start文件位于/etc/rc.d/init.d下,名为apached, 注意要可执行 ...

  4. 20160727noip模拟赛zld

    首先最优策略肯定是这样的:我们取出这个序列中的最大值,然后将整个序列分为左右两部分, 那么我们一定先把左右两部分合起来然后再与这个值合并 那么我们可以得出一个基于最值查询(rmq)的的算法,但是zld ...

  5. [转载]WebBrowser控件表单(form)的自动填写和提交

    话说有了WebBrowser类,终于不用自己手动封装SHDocVw的AxWebBrowser这个ActiveX控件了.这个类如果仅仅作为一个和IE一模一样浏览器,那就太没意思了(还不如直接用IE呢). ...

  6. [转载]C# Double toString保留小数点方法

    有时候double型数据需要toString(),但又想保留小数,当值为整数,比如3.00时tostring后会变为”3″,具体说明见下: 1 string str0 = i.ToString(&qu ...

  7. POJ 1026 Cipher(置换群)

    题目链接 题意 :由n个数字组成的密钥,每个数字都不相同,都在1-n之间,有一份长度小于等于n的信息,要求将信息放到密钥下边,一一对应,信息不足n的时候补空格,然后将位置重新排列,将此过程重复k次,求 ...

  8. POJ1416Shredding Company

    http://poj.org/problem?id=1416 题意 : 要为碎纸机公司开发一种新的碎纸机,这种碎纸机要具有3个特性 :一是粉碎机以一个目标数 t 作为输入,并且粉碎的纸上写有一个数字n ...

  9. TVM 下载监控

    iftop -n -i em1 -F 218.26.188.220/32 到10.10.81.23上用上面的语句看山西的流量. 到/opt/data/log/下看日志 到/opt/data/tvmca ...

  10. 【POJ3358】

    题目描述: 题意: 就是给定一个a/b,求a/b的结果变成二进制之后的小数.这个小数后面会有一段循环节,只要求输出循环节开始循环的位置和循环长度. 分析: 这题我是这么想的,比如说样例中的1/5,我们 ...