i am using Alert-dialog in Asynchronous task onPostexecute method.but my problem is when i come back to previous activity before the Asynchronous task completes.The Alertdialog (alert.show) is giving the following exception.The Alertdialog will appear on window but why this is not showing in other screens when i switch back.
Logcat Error:
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)
Code
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();
}
you must call the alertdialog on onprogressupate mtd as its call on main thread.
The possible reason is the context of the alert dialog. You may be finished that activity so its trying to open in that context but which is already closed. Try changing the context of that dialog to you first activity beacause it won't be finished till the end.
Try below code e.g
rather than this.
try to use
You're trying to show a dialog after your activity has lost visibility. You either need to not call show (which can be a PITA to catch), or you need to catch that exception and provide a reasonable behavior for your app instead. I've generally just caught the exception, but that may or may not be sufficient for you.
The problem is because in the onPostExecute(), you have the alert dialog and you are giving the context of Email Activity. But you are navigating to the other activity, so the context is wrong. Hence you are getting this error!
Better to show a progress and allow the user to navigate after the asynctask is done executing. If you want it to run in the background, use a Service.
Try out as below: