在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 ...
随机推荐
- 朴素的UNIX之-调度器细节
0.多进程调度的本质 我们都知道UNIX上有一个著名的nice调用.何谓nice,当然是"好"了.常规的想法是nice值越大越好,实际上,nice值越好,自己的优先级越低.那么为何 ...
- UVA - 1642 Magical GCD 数学
Magical GCD The Magical GCD of a nonempty sequence of positive integer ...
- oracle 11gR2 如何修改scan vip 地址 /etc/hosts方式
这次帮客户搭建了一套oracle 11gR2 rac for aix环境,scan vip因为网络调整需要,需要更改以前设置好的scan vip,是采用/etc/hosts的方式,比如将scan vi ...
- DB-MySql:MySQL 及 SQL 注入
ylbtech-DB-MySQL:MySQL 及 SQL 注入 1.返回顶部 1. MySQL 及 SQL 注入 如果您通过网页获取用户输入的数据并将其插入一个MySQL数据库,那么就有可能发生SQL ...
- Java-SpringCloud:目录
ylbtech-Java-SpringCloud:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:htt ...
- oracle (9I/10G/11G)数据库日志挖掘(审计误操作)
文档结构: 资料来自官方网站: https://docs.oracle.com/cd/E11882_01/server.112/e22490/logminer.htm#SUTIL019 来自论坛: h ...
- Linux 系列- 基本命令
Linux 基本命令 转自:http://www.taobaotest.com/blogs/qa?bid=353 Linux是一个基于命令的系统,它有很多很强的命令. 但它也有桌面系统,比如KDE, ...
- 开发工具 | 利用 deployd 搭建个人博客
前端er,通过利用deployd + mongodb,实现可视化的接口编写,自定义mock数据,数据存储在mongodb中:deployd目前还没有中文网,更多信息请参考http://deployd. ...
- php 生成不重复的随机字符串
md5(uniqid(md5(microtime(true)),true))
- SpringMVC视频
视频内容: 1.下载spring mvc以及spring mvc示例演示http://pan.baidu.com/s/1kTHRfDH 2.配置完善&初步探究控制器拦截http://pan.b ...