AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a
最近遇到一个奇葩的问题,好郁闷 之前也没有仔细看。问题偶尔出现一次。再去查看日志时,出现
view.WindowManager$BadTokenException: Unable to add window…is not valid; is your activity running?
什么情况,activity is runing? 对呀,activity没有destory呀,在跑呢,怎么会出现 对话框 附加到activity上加不了呢,还说无效的,似是activity被destory了呢。
后面一一检查 代码,确实 存在 activity 被destory时,异常还没有捕获到,而异步的AsynTask 还在运行呢。之后异常被捕获到了,再用对话框显示错误时,就会报错了,activity都死了,对话框还能活呀,再说此时提示已经没有意义了。应该丢掉 放弃 后续的处理。
那么 其实只需要 在异常 任务中 加上 isFinishing() 判断下,若activity挂了,就不管了。...
new AsyncTask<Void, Void, Object>(){
boolean isNoNetException = false;
boolean isNetLagException = false; //网络超时等不稳定 异常
boolean isConnException = false; //连接掉了,需要重新登录
@Override
protected void onPreExecute() {
//TODO 清空掉 所有的已填写的数据
pd = new ProgressDialog(SCInfoGainActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage(getString(R.string.pop_window_waiting_submit_tip));
pd.show();
}
@Override
protected Object doInBackground(Void... params) {
DataService ds = new DataService(SCInfoGainActivity.this);
Object retVal = null;
try {
retVal = ds.persubmitServiceChangeInfo(checkPhoneCustInfo.getUserId(), servicenum, checkPhoneCustInfo.getNettype(), serviceLists);
}catch (HessianConnectionException e) {
if(!DeviceUtil.isConnect(SCInfoGainActivity.this)){
//没网络发生的异常
isNoNetException = true;
}else{
//网络掉线了,请重新
isNetLagException = true;
}
}catch (SocketTimeoutException e) {
L.v(SCInfoGainActivity.class.getSimpleName(), "SocketTimeoutException ... "+e.getLocalizedMessage());
if(!DeviceUtil.isConnect(SCInfoGainActivity.this)){
isNoNetException = true; //没网络发生的异常
}else{
isNetLagException = true; //网络掉线了不稳定,请重新
}
e.printStackTrace();
}catch (ConnectException e){
L.v(SCInfoGainActivity.class.getSimpleName(), "SocketTimeoutException ... "+e.getLocalizedMessage());
if(!DeviceUtil.isConnect(SCInfoGainActivity.this)){
isNoNetException = true; //没网络发生的异常
}else{
isConnException = true; //网络掉线了不稳定,请重新
}
}catch (Exception e) {
//不是连接上的异常,比较复杂 有各种服务端返回的异常信息....
e.printStackTrace();
}
return retVal;
}
@Override
protected void onPostExecute(Object result) {
if(isFinishing()) return ; //若activity 都没有了,没有必要弹对话框了
pd.dismiss();
if(isNoNetException){
tipDialog.setContent(getString(R.string.tip_window_checknet));
tipDialog.show();
}else if(isNetLagException){
tipDialog.setContent(getString(R.string.tip_window_nostable_net));
tipDialog.show();
}else if(isConnException){
tipDialog.setContent(getString(R.string.tip_window_loseconn));
tipDialog.show();
}else{
handlePersubmitOrder(result);
}
}
}.execute();
AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a的更多相关文章
- 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 ...
- bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token
========4 关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...
- Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
在广播中启动一个Dialog时出现如下错误信息:Caused by: android.view.WindowManager$BadTokenException: Unable to add windo ...
- 关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window
下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token ...
- android.view.WindowManager$BadTokenException: Unable to add window
这是在加载dialog时出现的一个异常.转载地址:http://hi.baidu.com/fbdfp/item/7dea2d0ade9121813d42e23d 扔了好久的android又开始断断续续 ...
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
原博客地址:http://aijiawang-126-com.javaeye.com/blog/662336 在Activity中newSpinner是我把mContext传入,但是出了 andr ...
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic
之前遇到过这样的问题, 04-12 10:40:33.302: E/AndroidRuntime(17213): Caused by: android.view.WindowManager$BadTo ...
- Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added
很久之前测试通过的代码,现在手机升级了Android7.0后一运行就崩溃,报出这样的错误,具体错误如下: Process: com.example.sho.android_anti_theft, PI ...
- WindowManager$BadTokenException: Unable to add window permission denied for this window type
10-11 11:47:27.472: E/AndroidRuntime(12804): java.lang.RuntimeException: Unable to start activity Co ...
随机推荐
- Android Studio:Error:Execution failed for task ':app:mergeDebugResources'. > Some file crunching failed, see logs for details
Gradle 编译错误: 14:39:58 Executing tasks: [clean, :app:generateDebugSources, :app:mockableAndroidJar, : ...
- 第31讲 UI组件之 Gallery画廊控件
第31讲 UI组件之 Gallery画廊控件 1.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery只 ...
- hdu 5422 Rikka with Graph(简单题)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- iOS 语音识别使用讯飞报错
You must rebuild it with bitcode enabled(Xcode setting ENABLE_BITCODE), obtain an updated library fr ...
- Java操作属性文件,支持新增或更新多个属性
Java操作属性文件.支持新增或更新多个属性 一.更新或新增单个属性的方法 /** * 写入properties信息 * @param filePath 绝对路径(包含文件名称和后缀名) * @par ...
- LR翻页脚本并在每页实现业务操作
性能需求:在列表中删除后有记录,或对列表中的每条记录进行操作(如点击每条记录的“单号”进入订单详情页面,或在列表中对每条记录进行“启用”.“停止”操作) 举例:Vuser脚本模拟用户在订单列表中点击每 ...
- CodeSmith使用总结--调用自定义方法
上一篇读取了一个表的内容,但是到了真正应用的时候还是不够用的,我们很容易可以对比出来,SQL里边的数据类型的定义和C#中有所不同,比如Saler--String,大写的String在C#中不是一个类型 ...
- 普通用户之间的ssh无密码访问设置方法
两台CentOS6.2服务器,客户端是node1,服务器是node2,先都用root用户配置,方法如下: 第一步:在客户端Node1:生成密匙对,我用的是rsa的密钥.使用命令 "ssh-k ...
- HTML与CSS入门——第十章 创建用于Web上的图像
知识点: 1.选择图像软件的方法 2.准备用于网上的照片的方法 3.创建标题和按钮的方法 4.减少图像中颜色数量的方法 5.创建透明图像的方法 6.创建平铺背景的方法 7.创建Web动画的方法 10. ...
- asp.net获取ip地址的方法
在ASP中使用 Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的I ...