今天在使用Android弹出对话框的时候,报了一个unable to add window错误,我的代码如下

new AlertDialog.Builder(getApplicationContext()).setTitle("提示").setMessage("你确定要删除么?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();

仔细看代码,一切都很正常,也没找到什么错误,但是我们忽略了只有Activity才能添加window,所以上面的代码如果放在onCreate()方法中,我们把 Builder(getApplicationContext())改成Builder(this)即可,如果我们是在其他方法中使用,就不能使用getApplicationContext(),而应该使用Builder(Activity.this)


												

Android 使用弹出对话框,报Unable to add window错误的更多相关文章

  1. Android 按钮 弹出对话框

    ackage com.example.helloworld; import android.os.Bundle;import android.app.Activity;import android.a ...

  2. Android 手机卫士--弹出对话框

    在<Android 手机卫士--解析json与消息机制发送不同类型消息>一文中,消息机制发送不同类型的信息还没有完全实现,在出现异常的时候,应该弹出吐司提示异常,代码如下: private ...

  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. 如何在service实现弹出对话框

           因为一些需求,我想在service处理后台运行时候,会弹出对话框,但是对话框的建立需要传入Context的值,我试过传入this,也就是service自己的context,还有 传入ge ...

  5. AlertDialog.Builder弹出对话框

    在Android中,弹出对话框使用AlertDialog.Builder方法. new AlertDialog.Builder(MainActivity.this).setTitle("本机 ...

  6. service里面弹出对话框

    如何在service里面弹出对话框先给一个需求:需要在service里面监听短信的接收,如果接收到短信了,弹出一个dialog来提示用户打开. 看看效果图:(直接在主桌面上弹出) service中弹出 ...

  7. Android屏幕底部弹出DialogFragment(3)

     Android屏幕底部弹出DialogFragment(3) 附录文章1,2的DialogFragment是常规的DialogFragment,但是现在的一些Android开发中,往往需要从底部 ...

  8. 安卓弹出对话框——Alertdialog

    在Android开发当中,在界面上弹出一个Dialog对话框使我们经常需要做的,本篇随笔将详细的讲解Dialog对话框这个概念,包括定义不同样式的对话框. 一.Dialog 我们首先来看看androi ...

  9. 安卓弹出对话框——Alertdialog(一)

    首先看各种样式的对话框: 我们看到,Dialog有很多的子类实现,所以我们要定义一个对话框,使用其子类来实例化一个即可,而不要直接使用Dialog这个父类来构造. 二.AlertDialog 今天我们 ...

随机推荐

  1. Technology Trader

    zoj2071:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2071 题意:题意一些零件,每一个零件会有一个花费,然后用这 ...

  2. Hidden Password

    zoj1729:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=729 题意:就是求字符串的最小表示,模板题. 题解:直接贴模板. ...

  3. 【POI2003/2004 stage I】

    [原题在此] Let us consider a game on a rectangular board m x 1 consisting of m elementary squares number ...

  4. 【UVA1579】俄罗斯套娃 Matryoshka (动态规划)

    题目: 分析: 其实就是两个dp结合起来.第一个dp求区间[l,r]全部合并起来要用的最小次数,可以用区间[l,k]&[k+1,r]转移(l<=k<r).第二个dp求前i个娃娃分成 ...

  5. Eclipse 项目有红感叹号、小红叉

    红感叹号: 问题原因]:工程中classpath中指向的包路径错误 [解决办法]:右键项目名称 BuildPath ---> Configure Build Paht...中,然后上面有几个选项 ...

  6. Android 正则表达式匹配汉字中文

    关于中文的正则表达式, 应该是^[\\u4E00-\\u9FFF]+$, 和论坛里常被人提起的^[\\u4E00-\\u9FA5]+$很接近需要注意的是论坛里说的^[\\u4E00-\\u9FA5]+ ...

  7. easy_install和pip区别

    easy_insall的作用和perl中的cpan, ruby中的gem类似,都提供了在线一键安装模块的傻瓜方便方式,而pip是easy_install的改进版, 提供更好的提示信息,删除packag ...

  8. 14.8.4 Moving or Copying InnoDB Tables to Another Machine 移动或者拷贝 InnoDB 表到另外机器

    14.8.4 Moving or Copying InnoDB Tables to Another Machine 移动或者拷贝 InnoDB 表到另外机器 这个章节描述技术关于移动或者复制一些或者所 ...

  9. 利用rowid更新单表

    SQL> create table test1(id int,name char(10)); Table created. begin for i in 1 .. 1000000 loop in ...

  10. 【转】蓝牙物理链路类型:SCO和ACL链路

    原文网址:http://blog.chinaunix.net/uid-23193900-id-3272233.html  蓝牙物理链路ACL(Asynchronous Connectionless), ...