异常类型为:android.view.WindowManager$BadTokenException:
Unable to add window

我在fragment中public
View onCreateView(LayoutInflater
inflater, ViewGroup
container,Bundle
savedInstanceState) {中试图调用对话框静态工具类时出现该异常:当时上下文环境使用

context = getActivity().getApplicationContext();

调用方法为:

 mp = new MyProgress(context);
mp.start();

静态工具类为:

package com.srit.zcxc.myview;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView; public class MyProgress extends View { private AlertDialog.Builder builder = null;
private AlertDialog dialog = null;
private LinearLayout linear = null; public MyProgress(Context context) {
super(context);
builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
linear = new LinearLayout(context);
linear.setOrientation(LinearLayout.VERTICAL);
linear.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
linear.setBackgroundColor(Color.BLACK);
linear.setGravity(Gravity.CENTER);
linear.setPadding(5, 5, 5, 5);
ProgressBar pb = new ProgressBar(context);
linear.addView(pb); TextView tv = new TextView(context);
tv.setGravity(Gravity.CENTER);
tv.setText("取消登录");
tv.setTextColor(Color.WHITE);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stop();
}
});
linear.addView(tv);
builder.setView(linear);
} public void setBackgroundDrawableByresourceId(int resourceId) {
linear.setBackgroundResource(resourceId);
} public void start() {
dialog = builder.show();
} public void stop() {
if (dialog.isShowing()) {
dialog.dismiss();
}
} public boolean isShowing() {
return dialog.isShowing();
}
}

解决方法为:

            mp = new MyProgress(this.getActivity());
mp.start();

产生异常的原因:

获取上下文this.getApplicationContext())和 this的差别:

这里的this指的当然就是Acitivity.this , 指的是这个Acitivity的上下文。而this.getApplicationContext()指的则是整个应用

的上下文。

对于AlertDialog来说,是须要依赖一个View,而View是相应于Activity的。

那么为什么会报错呢,这里涉及到一个生命周期的问题了。

对于一个应用Context来说。它的生命周期是整个应用程序的生命周期。而对于Activity来说。当它销毁之后它的生命

周期就结束了。

AlertDialog是属于Acitivity的,当Activity销毁的时候它也必须销毁,所以这里我们指定是Activity的Context。

在fragment中显示对话框出现异常的更多相关文章

  1. 如何在服务(Service)程序中显示对话框

    原文:http://www.vckbase.com/index.php/wv/94 服务程序(Service)一般是不能和用户进行交互的,所以他一般是不能显示窗口的.要和用户进行交互(如显示窗口),我 ...

  2. MFC对话框中显示BMP,JPG图片

    //************************************ // 方法说明:    显示JPG和GIF.BMP图片 // 参数说明:    CDC * pDC           设 ...

  3. fragment中嵌套viewpager,vierpager中有多个fragment,不显示 .

    fragment中嵌套viewpager,vierpager中有多个fragment,不显示 ... 现在好多应用流行一种布局.底部几个工具栏选项,上面也有类似tab的选项. 底部用RadioGrou ...

  4. 如何在VBS脚本中显示“选择文件对话框”或“选择目录对话框”

    .选择文件[XP操作系统,不能用于Win2000或98],使用“UserAccounts.CommonDialog”对象向用户显示一个标准的“文件打开”对话框 Set objDialog = Crea ...

  5. MFC对话框中显示背景图片

    在MFC对话框中显示图片,四个步骤. 1.首先得在VC6.0或者VS2008(其他版本也是一样)中导入GDI文件.(网上下载:) GDI含义是图形设备接口,主要任务是负责系统与绘图程序之间的信息交换, ...

  6. 【代码笔记】iOS-在导航栏中显示等待对话框

    一,效果图. 二,代码. ViewController.m #import "ViewController.h" @interface ViewController () @end ...

  7. C#.NET中使用BackgroundWorker在模态对话框中显示进度条

    这里是一个示例,其中展示了如何使用Backgroundworker对象在模态对话框中显示后台操作的实时进度条. 首先是主窗体代码: using System; using System.Collect ...

  8. How to: Display a List of Non-Persistent Objects in a Popup Dialog 如何:在弹出对话框中显示非持久化对象列表

    This example demonstrates how to populate and display a list of objects that are not bound to the da ...

  9. activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI

    今天遇到挺纠结的问题,由于产品设计的问题,技术上涉及到activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI ...

随机推荐

  1. 移动端js手指滑动事件初体验

    今天在公司遇到做一个移动端手指滑动的效果,刚開始用了swiper.js插件发现效果不好(文字存在模糊效果).后来查了一些资料,自己手写了一个使用原生js写的滑动效果. 以下直接上代码: <!do ...

  2. 【MongoDB】The basic operation of Index in MongoDB

    In the past four blogs, we attached importance to the index, including description and comparison wi ...

  3. Tom和Jerry来了,Tom和Jerry走了——北漂18年(38)

    上次讲到跟我同一时候入职的女销售走了. 回忆起来,她的问题多半是技巧足够,脑子不足够,走了之后再没联系.不久之后,在老板的要求之下.LilyG又招聘了两位男销售,英文名字非常登对一个叫Tom,一个叫J ...

  4. hdu 1166 敌兵布阵——(区间和)树状数组/线段树

    pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...

  5. POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 53 ...

  6. Qt-信号和槽-多对多

    前言:介绍1对多,多对1以及多对多的案例. 一.1对多 演示内容:在QLineEdit输入时,同步label,text browser以及调试输出板同步显示. 1.1 新建工程 1.2 添加部件 拖入 ...

  7. 关于TCP的三次握手和四次分手 专题

    客户端TCP状态迁移:CLOSED->SYN_SENT->ESTABLISHED->FIN_WAIT_1->FIN_WAIT_2->TIME_WAIT->CLOSE ...

  8. synchronized同步机制,修饰类和修饰对象的区别

    synchronized用法 synchronized修饰的对象有几种: 修饰一个类:其作用的范围是synchronized后面括号括起来的部分,作用的对象是这个类的所有对象: 修饰一个方法:被修饰的 ...

  9. 使用最新vue_cli搭建的模版

    使用最新vue_cli搭建的模版,包含了常用的插件,router和axiox与测试插件.项目的结构如下: 使用之前请打开 REAMME.md 看看. 已经搭建好的框架的下载地址:https://sha ...

  10. JAVA实现两种方法反转单列表

    /** * @author luochengcheng * 定义一个单链表 */ class Node { //变量 private int record; //指向下一个对象 private Nod ...