0

我正在尝试放置一个sweet alerthttps://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一个,我将它作为参数发送。ClassAsycTask

和我的错误日志:

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 任务:

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

RegisterApp 构造函数

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;
    }
4

2 回答 2

1

请检查 runOnUiThread。

runOnUiThread(new Runnable() {
@Override
public void run() {
    // Your code for UI event in AsyncTask
} });
于 2015-05-14T02:13:18.263 回答
0

看起来您的 Activity Context 可能为空,可能是由于 Activity 被破坏。

执行以下命令时尝试使用getApplicationContext()而不是dialogReserva.thisfor :ctxAsyncTask

new RegisterApp((getApplicationContext(), gcm, getAppVersion(getApplicationContext()), String.valueOf(getIntent().getIntExtra("idviaje", 0)), userInput.getText().toString()).execute();
于 2015-05-14T02:52:19.247 回答