android.view.WindowManager$BadTokenException: Unable to add window异常
2013-05-03 08:24
239 查看
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@40514f68 is not valid; is your activity running?
因为使用了AsyncTask 异步线程在线程完成以后的onPostExecute方法里面弹出窗口。
这个时候如果用户在onPostExecute调用之间按了返回按钮,activity已经onDestory了,
那么就会报出android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4479b390 is not valid; is your activity running?
解决方法一在弹出窗口之前用Activity的isFinishing判断一下Activity是否还存在
protected void onPostExecute(Object result) {
if (!isFinishing()) {
showDialog(MY_DIALOG_ID);
}
}
解决方法二在show的时候捕获一下异常
以下是测试验证
运行这个小程序 默数4的时候按返回 如果不加异常捕获或者判断isFinish的话会崩溃。
public class DialogTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Task().execute();
}
private class Task extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//if (!isFinishing()) {
//try {
createAlertDialog().show();
//} catch (Exception e) {
//}
//}
}
}
private AlertDialog createAlertDialog() {
return new AlertDialog.Builder(DialogTestActivity.this).setTitle("fadfasdf")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
}
}
因为使用了AsyncTask 异步线程在线程完成以后的onPostExecute方法里面弹出窗口。
这个时候如果用户在onPostExecute调用之间按了返回按钮,activity已经onDestory了,
那么就会报出android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4479b390 is not valid; is your activity running?
解决方法一在弹出窗口之前用Activity的isFinishing判断一下Activity是否还存在
protected void onPostExecute(Object result) {
if (!isFinishing()) {
showDialog(MY_DIALOG_ID);
}
}
解决方法二在show的时候捕获一下异常
以下是测试验证
运行这个小程序 默数4的时候按返回 如果不加异常捕获或者判断isFinish的话会崩溃。
public class DialogTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Task().execute();
}
private class Task extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//if (!isFinishing()) {
//try {
createAlertDialog().show();
//} catch (Exception e) {
//}
//}
}
}
private AlertDialog createAlertDialog() {
return new AlertDialog.Builder(DialogTestActivity.this).setTitle("fadfasdf")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
}
}
相关文章推荐
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootI
- android.view.WindowManager$BadTokenException
- [crash 分析] android.view.WindowManager$BadTokenException: Unable to add window
- Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.B
- android.view.WindowManager$BadTokenException: Unable to add window
- 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? -
- android.view.WindowManager$BadTokenException: Unable to add window-permission denied 完美兼容8.0的系统
- android.view.WindowManager$BadTokenException: Unable to add window — token null
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- AlertDialog.Builder发生:android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window -- token
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- 解决: android.view.WindowManager BadTokenException:Unable to add Window.Is your activity running?
- bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token
- Android实现浮窗时报的android.view.WindowManager$BadTokenException: Unable to add window异常
- 解决android.view.WindowManager$BadTokenException: Unable to add window -- token .
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException