异常类型为: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. 交叉编译faac共享库

    作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 Advanced Audio Coding.一种专为声音数据设计的文件压缩格式,与Mp3不同,它採 ...

  2. FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT

    FATAL ERROR in native method:JDWP No transports initialized,jvmtiError=AGENT_ERROR_TRANSPORT_INIT(19 ...

  3. C# 跨线程调用form控件技巧及byte[]与string型相互转换

    跨线程调用form控件技巧 private delegate void MethodSocket(object obj);//使用托管 ss = "OK"; this.BeginI ...

  4. Java集合(二):List列表

    在上一节中,介绍了Java集合的总体情况.从这节開始,将介绍详细的类.这里不单单介绍类的使用方法.还会试图从源代码的角度分析类的实现.这一节将介绍List接口及实现类.即列表中的链表LinkedLis ...

  5. 【PLSQL】触发器trigger类型,状态,參数

    ************************************************************************   ****原文:blog.csdn.net/clar ...

  6. Codeforces Gym 100015F Fighting for Triangles 状态压缩DP

    F Fighting for Triangles Description Andy and Ralph are playing a two-player game on a triangular bo ...

  7. USACO 2.1 Ordered Fractions

    Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominat ...

  8. http server优雅启停原理及验证

    问题背景 在 http应用程序重启时,如果直接 kill -9 使程序退出,然后再启动,会存在的问题: 旧的请求未处理完,如果服务端进程直接退出,会造成客户端连接中断(收到 RST) 新请求打过来,服 ...

  9. mybatis通用的crud的接口

    http://git.oschina.net/jrl/mybatis-mapper https://www.oschina.net/p/mybatis-plus

  10. Android设计模式—— 观察者模式(以及EventBus的简单使用)

    1.概述 观察者模式最常用的地方是GUI系统.订阅-发布系统.观察者模式的一个重要作用就是解耦,将观察者和被观察者解耦,使得他们之间的依赖性更小. 2.定义 定义对象间的一种一对多依赖关系,使得每当一 ...