我在异步任务 onPostexecute 方法中使用警报对话框。但我的问题是当我在异步任务完成之前回到以前的活动时。警报对话框(alert.show)给出以下异常。警报对话框将出现在窗口上但为什么当我切换回来时,这不会显示在其他屏幕上。
日志错误:
02-27 05:03:05.283: E/AndroidRuntime(827): FATAL EXCEPTION: main
02-27 05:03:05.283: E/AndroidRuntime(827): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@40f25058 is not valid; is your activity running?
02-27 05:03:05.283: E/AndroidRuntime(827): at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.app.Dialog.show(Dialog.java:281)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
02-27 05:03:05.283: E/AndroidRuntime(827): at com.example.parentportal.Email$emailtask.onPostExecute(Email.java:119)
02-27 05:03:05.283: E/AndroidRuntime(827): at com.example.parentportal.Email$emailtask.onPostExecute(Email.java:1)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.os.Looper.loop(Looper.java:137)
02-27 05:03:05.283: E/AndroidRuntime(827): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-27 05:03:05.283: E/AndroidRuntime(827): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 05:03:05.283: E/AndroidRuntime(827): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 05:03:05.283: E/AndroidRuntime(827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 05:03:05.283: E/AndroidRuntime(827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 05:03:05.283: E/AndroidRuntime(827): at dalvik.system.NativeStart.main(Native Method)
代码
Context mContext = Email.this;
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
alert.setTitle("");
WebView wv = new WebView(mContext);
String html = "<h3>Message has been sent successfully</h3>";
wv.loadData(html, "text/html", "UTF-8");
alert.setView(wv);
// alert.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
// public void onClick(DialogInterface dialog, int id){
//
// }
// });
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
}
});
alert.show();
}