原博客地址: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. LoadRunner 学习笔记(3)基础

    LoadRunner包含的组件: Virtual User Generator 录制最终用户业务流程并创建自动化性能测试脚本,即 Vuser 脚本 Controller 组织.驱动.管理并监控负载测试 ...

  2. Hadoop启动异常情况解决方案

    1. 启动时报WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using b ...

  3. 仅仅测试Word2016发布博客

    我来啦! 我走啦!     哈哈哈!   int main(int argc, char *argv[]) {     int mysocket; //建立一个socket后返回的值是int类型的. ...

  4. c++ break while

    #include <iostream> #include <vector> #include <pthread.h> #include "destory_ ...

  5. Window7中Eclipse运行MapReduce程序报错的问题

    按照文档:http://www.micmiu.com/bigdata/hadoop/hadoop2x-eclipse-mapreduce-demo/安装配置好Eclipse后,运行WordCount程 ...

  6. Linq常用

    1.左关联查询var lst = from m in db.信息          join d in db.明细信息          on m.单号 equals d.单号 into mi     ...

  7. Matlab中cellfun函数的使用

    Compute the mean of each vector in cell array C. C = {1:10, [2; 4; 6], []}; averages = cellfun(@mean ...

  8. 01-05-01-1【Nhibernate (版本3.3.1.4000) 出入江湖】延迟加载及其class和集合(set、bag等)的Lazy属性配置组合对Get和Load方法的影响

    这篇文章 http://ayende.com/blog/3988/nhibernate-the-difference-between-get-load-and-querying-by-id One o ...

  9. Relearning PHP (2) – php 的浮点数float

    Relearning PHP (2) – php 的浮点数float 暂无评论 php有很多坑,但是并不妨碍他是最好的语言.其他语言对于浮点数处理同样有问题,这应该是个“共有坑”.不信可以用googl ...

  10. 如何在winform DataGridView控件的DataGridViewButtonColumn按钮列中禁用按钮

    原文:http://msdn.microsoft.com/en-us/library/ms171619(v=vs.85).ASPX public class DataGridViewDisableBu ...