我正在尝试放置一个sweet alert
https://github.com/pedant/sweet-alert-dialog库,但它的工作原理很完美,但不是AsyncTask
。
我的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是Context
的{{1}},我执行Class
并将其作为参数发送。
和我的错误日志:
AsycTask
ctx赋值:
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)
RegisterApp构造函数
new RegisterApp(dialogReserva.this, gcm, getAppVersion(getApplicationContext()), String.valueOf(getIntent().getIntExtra("idviaje", 0)), userInput.getText().toString()).execute();
答案 0 :(得分:0)
请检查runOnUiThread。
runOnUiThread(new Runnable() {
@Override
public void run() {
// Your code for UI event in AsyncTask
} });
答案 1 :(得分:0)
看起来您的活动上下文可能为null,可能是由于活动被销毁。
执行getApplicationContext()
后,尝试使用dialogReserva.this
代替ctx
AsyncTask
:
new RegisterApp((getApplicationContext(), gcm, getAppVersion(getApplicationContext()), String.valueOf(getIntent().getIntExtra("idviaje", 0)), userInput.getText().toString()).execute();