The fact is that I have a ProgressDialog
within a class AsyncTask
and the case is that when it is executed, to make a connection, the first time I get the Progress correctly, but if I do a search again (this is what the Asynctask
is responsible for) I get the following error:
FATAL EXCEPTION: main Process: com.xxxx.cccc, PID: 25502 android.view.WindowManager $ BadTokenException: Unable to add window - token null is not valid; is your activity running? at android.view.ViewRootImpl.setView (ViewRootImpl.java:765) at android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:356) at android.view.WindowManagerImpl.addView (WindowManagerImpl.java:93) at android.app.Dialog.show (Dialog.java:330) at com.xxxx.cccc.conexion.CRC.onPreExecute (CRC.java:79) at android.os.AsyncTask.executeOnExecutor (AsyncTask.java:648) at android.os.AsyncTask.execute (AsyncTask.java:595) at com.xxxx.cccc.listas.LCNA $ 5.onClick (LCNA.java:127) at android.view.View.performClick (View.java:6294) at android.view.View $ PerformClick.run (View.java:24770) at android.os.Handler.handleCallback (Handler.java:790) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loop (Looper.java:164) at android.app.ActivityThread.main (ActivityThread.java:6494) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
This is the code:
public class xxxx extends AsyncTask<String, Void, String> {
...
private ProgressDialog progressDialog;
...
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(contexto);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(true);
progressDialog.setIndeterminate(false);
progressDialog.setMessage("Conectando con el servidor. Espere por favor...");
progressDialog.show();
}
@Override
protected String doInBackground(String... values) {
...
if (conectarServidor()) {
publishProgress();
transferenciaInformacion(); //Recibe información del Servidor y lo trata
this.cancel(true);
} else {
publishProgress();
}
return "true";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.cancel();
}
@Override
protected void onCancelled(String s) {
progressDialog.cancel();
super.onCancelled(s);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
/**
* Condición de que no haya logrado conectarse con el servidor
* En cuyo caso lanzará un AlertDialog advirtiéndolo
* Si se ha logrado conectar no se lanzará el AlertDailog
* Además lanzará un mensaje de Error dependiendo de si ha sido por error de conexión o porque se ha agotado el tiempo de espera de conexión
*/
if (!isConectado && isTimeOut) {
Handler handler = new Handler(contexto.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(contexto, "Error de conexión. No se pueden cargar los articulos", Toast.LENGTH_LONG).show();
}
});
} else if (!isConectado && isIOError) {
Handler handler = new Handler(contexto.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(contexto, "Error de conexión. No se pueden cargar los articulos", Toast.LENGTH_LONG).show();
}
});
}
}
...
The call to the method:
XXXXX xxxx= new XXXXX(contexto);
xxxx.execute("","");
This is located on a button that is not clicked again until the entire process done by the AsyncTask has been completed
The code that is with XX or with rare characters is irrelevant, I have removed the excess code that is irrelevant for the case.