0

I'm trying to put a sweet alert of https://github.com/pedant/sweet-alert-dialog library and it works perfect, but not in an AsyncTask.

my onPostExcute:

protected void onPostExecute(String result) {

        super.onPostExecute(result);
        if(!Pattern.compile("0").matcher(msj).find())
        {
            Toast.makeText(ctx, "¡Ya tienes una reserva para este viaje!", Toast.LENGTH_SHORT).show();
        }else if(Pattern.compile("200").matcher(msj).find()) {
            SweetAlertDialog swt = new SweetAlertDialog(ctx, SweetAlertDialog.WARNING_TYPE)
                    .setTitleText("Todas las plazas ocupadas")
                    .setContentText("¿Desea entrar en la lista de espera? Será avisado en cuanto haya una plaza")
            .setCancelText("No")
            .setConfirmText("Si")
            .showCancelButton(true)
            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sDialog) {
                    // reuse previous dialog instance, keep widget user state, reset them if you need
                    sDialog.cancel();
                    Toast.makeText(ctx, "Reserva no realizada", Toast.LENGTH_SHORT).show();

                }
            })
            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sDialog) {
                sDialog.cancel();
                }
            });
    swt.show();
 }

ctx is a Context of a Class where I execute the AsycTask and I send it as parameter.

and my error log:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ********, PID: 6563
    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:566)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:298)
            at *************.RegisterApp.onPostExecute(RegisterApp.java:175)
            at *************.RegisterApp.onPostExecute(RegisterApp.java:40)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

ctx assignment:

new RegisterApp(dialogReserva.this, gcm, getAppVersion(getApplicationContext()), String.valueOf(getIntent().getIntExtra("idviaje", 0)), userInput.getText().toString()).execute();

RegisterApp constructor

public class RegisterApp extends AsyncTask<Void, Void, String> {


    private static final String TAG = "GCMRelated";
    Context ctx;
    String id_viaje;
    String msj;
    String tlf;
    GoogleCloudMessaging gcm;
    private int appVersion;

    public RegisterApp(Context ctx, GoogleCloudMessaging gcm, int appVersion, String id_viaje, String tlf){
        this.ctx = ctx;
        this.gcm = gcm;
        this.id_viaje=id_viaje;
        this.tlf=tlf;
        this.appVersion = appVersion;
    }

2 Answers2

1

Please check runOnUiThread.

runOnUiThread(new Runnable() {
@Override
public void run() {
    // Your code for UI event in AsyncTask
} });
Charles
  • 139
  • 6
0

It looks like your Activity Context might be null, possibly due to the Activity being destroyed.

Try using getApplicationContext() instead of dialogReserva.this for ctx when you execute the AsyncTask:

new RegisterApp((getApplicationContext(), gcm, getAppVersion(getApplicationContext()), String.valueOf(getIntent().getIntExtra("idviaje", 0)), userInput.getText().toString()).execute();
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • oh, I have found hte solution I had a finish() method after execute the asynctask, so I did it in AsynTask and works fine. thanks you so! – Aitor Ramos Pajares May 14 '15 at 02:56
  • @AitorRamosPajares Yeah, that would explain it! Using the Application Context would allow you to keep the call to `finish()` where you had it before, since it is a valid Context as long as your app is running. – Daniel Nugent May 14 '15 at 02:59