在fragment中显示对话框出现异常
异常类型为: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中显示对话框出现异常的更多相关文章
- 如何在服务(Service)程序中显示对话框
原文:http://www.vckbase.com/index.php/wv/94 服务程序(Service)一般是不能和用户进行交互的,所以他一般是不能显示窗口的.要和用户进行交互(如显示窗口),我 ...
- MFC对话框中显示BMP,JPG图片
//************************************ // 方法说明: 显示JPG和GIF.BMP图片 // 参数说明: CDC * pDC 设 ...
- fragment中嵌套viewpager,vierpager中有多个fragment,不显示 .
fragment中嵌套viewpager,vierpager中有多个fragment,不显示 ... 现在好多应用流行一种布局.底部几个工具栏选项,上面也有类似tab的选项. 底部用RadioGrou ...
- 如何在VBS脚本中显示“选择文件对话框”或“选择目录对话框”
.选择文件[XP操作系统,不能用于Win2000或98],使用“UserAccounts.CommonDialog”对象向用户显示一个标准的“文件打开”对话框 Set objDialog = Crea ...
- MFC对话框中显示背景图片
在MFC对话框中显示图片,四个步骤. 1.首先得在VC6.0或者VS2008(其他版本也是一样)中导入GDI文件.(网上下载:) GDI含义是图形设备接口,主要任务是负责系统与绘图程序之间的信息交换, ...
- 【代码笔记】iOS-在导航栏中显示等待对话框
一,效果图. 二,代码. ViewController.m #import "ViewController.h" @interface ViewController () @end ...
- C#.NET中使用BackgroundWorker在模态对话框中显示进度条
这里是一个示例,其中展示了如何使用Backgroundworker对象在模态对话框中显示后台操作的实时进度条. 首先是主窗体代码: using System; using System.Collect ...
- 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 ...
- activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI
今天遇到挺纠结的问题,由于产品设计的问题,技术上涉及到activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI ...
随机推荐
- 深入浅出CChart 每日一课——快乐高四第九课 于无声处,CChart内置功能介绍之数据存取篇
笨笨长期以来一直使用Origin软件画图和处理数据,但Origin软件没有编程语言的接口.笨笨开发CChart的一个潜在的目标.是想实现Origin软件的功能.当然这是一个不可能达到的目标.Origi ...
- Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)
题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...
- Android顶部粘至视图具体解释
不知从某某时间開始,这样的效果開始在UI设计中流行起来了.让我们先来看看效果: 大家在支付宝.美团等非常多App中都有使用.要实现这个效果,我们能够来分析下思路: 我们肯定要用2个一样的布局来显示我们 ...
- 51nod-1363: 最小公倍数之和
[传送门:51nod-1363] 简要题意: 给出一个数n,求出1到n的数与n的最小公倍数的和 多组数据 题解: 理所当然推柿子 原题相当于求$\sum_{i=1}^{n}\frac{i*n}{gcd ...
- sklearn中的数据预处理----good!! 标准化 归一化 在何时使用
RESCALING attribute data to values to scale the range in [0, 1] or [−1, 1] is useful for the optimiz ...
- 杂项:WCF
ylbtech-杂项:WCF Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了 ...
- 40.DOM读取XML
main.cpp #include <QtGui> #include <iostream> #include "domparser.h" int main( ...
- ubuntu安装之后root用户配置
安装ubuntu之后发现不切换到root显示:su: Authentication failure 需要进行一下操作 表示成功切换到root用户
- C#中显现串口通信SerialPort类
SerialPort类的常用属性 名 称 说 明 BaseStream 获取 SerialPort 对象的基础 Stream 对象 BaudRate 获取或设置串行波特率 BreakState 获取或 ...
- 洛谷P3357 最长k可重线段集问题(费用流)
题目描述 给定平面 x-O-yx−O−y 上 nn 个开线段组成的集合 II ,和一个正整数 kk .试设计一个算法,从开线段集合 II 中选取出开线段集合 S\subseteq IS⊆I ,使得在 ...