Sweet alert in an AsyncTask Android -
i'm trying put sweet alert
of https://github.com/pedant/sweet-alert-dialog library , works perfect, not in 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 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 context
of class
execute asyctask
, send parameter.
and error log:
e/androidruntime﹕ fatal exception: main process: ********, pid: 6563 android.view.windowmanager$badtokenexception: unable add window -- token null not application @ android.view.viewrootimpl.setview(viewrootimpl.java:566) @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:272) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) @ android.app.dialog.show(dialog.java:298) @ *************.registerapp.onpostexecute(registerapp.java:175) @ *************.registerapp.onpostexecute(registerapp.java:40) @ android.os.asynctask.finish(asynctask.java:632) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ 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; }
it looks activity context might null, possibly due activity being destroyed.
try using getapplicationcontext()
instead of dialogreserva.this
ctx
when execute asynctask
:
new registerapp((getapplicationcontext(), gcm, getappversion(getapplicationcontext()), string.valueof(getintent().getintextra("idviaje", 0)), userinput.gettext().tostring()).execute();
Comments
Post a Comment