今天在使用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. LA 4975

    回文串的题,求最大的双重回文串: 重新复习了一下manacher算法: 代码: #include<cstdio> #include<cstring> #include<a ...

  2. astyle基本功能介绍

    astyle是一个命令行工具,语法格式如下 astyle [options] < original > 例如: astyle --style=ansi foo.cpp 上面的命令将美化fo ...

  3. 支付宝APP支付Java回调具体步骤

    /** * 支付宝异步请求通知 * * @param request * @return */@RequestMapping(value = "async", method = R ...

  4. 搜索(剪枝优化):HDU 5113 Black And White

    Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...

  5. UVA10054 The Necklace

    UVA10054 The Necklace 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18806 [思路] 欧拉回路 ...

  6. Reachability几个常用方法

    http://oncerios.diandian.com/post/2013-06-28/40050041969

  7. JavaScript高级程序设计20.pdf

    用户代理检测 为了不在全局作用域中添加多余的变量,我们使用模块增强模式来封装检测脚本 以下是完整的用户代理字符串检测脚本,包括检测呈现引擎.平台.Window操作系统.移动设备和游戏系统 var cl ...

  8. (转载)sinaeditor漏洞

    SinaEditor简介 SinaEditor是基于新浪博客编辑器的开源编辑器.您可以用它来编辑富文本内容. 编辑器的核心是一个执行队列的调度系统,加入插件来实现功能,并通过事件来驱动编辑器的运行.我 ...

  9. 通过mysql写入一句话木马

    USE mysql;# MySQL 返回的查询结果为空(即零行). # MySQL 返回的查询结果为空(即零行). CREATE TABLE a( cmd1 text NOT NULL );# MyS ...

  10. CentOS添加swap分区

    在多个VPS上尝试. 1.进入目录 cd /var/ 2.获取要增加的SWAP文件块(这里以1GB为例) dd if=/dev/zero of=swapfile bs=1024 count=10383 ...