为什么我的 ProgressDialog 在我的进程之后显示?

why my ProgressDialog shown after my process?

我不明白,为什么我的 ProcessDialog 在我的过程之后显示,我使用这个元素 (ProcessDialog) 并且工作正常但不是在这个方法中。

    public void prueba(View view) {
     Log.e("click","now");

    progress = ProgressDialog.show(RegistroAuto.this, "dialog title","dialog message", true);
    if (edtMiplaca.getText().toString().equalsIgnoreCase("")) {
        utilidades.mostrarAlerta(RegistroAuto.this, "", "Debe ingresar una placa");
    } else if

    (edtMiVehiculo.getText().toString().equalsIgnoreCase("")) {
        utilidades.mostrarAlerta(RegistroAuto.this, "", "Debe ingresar un nombre");
        } else {
        String placa = edtMiplaca.getText().toString();
        String nombre = edtMiVehiculo.getText().toString();
        if (utilidades.validaPlaca(placa) && utilidades.validaSoloAlfanumericos(placa)) {
            if (hp.existeSoloPlaca(placa)) {
                utilidades.mostrarAlerta(RegistroAuto.this, "", "La placa ya existe.");
                } else if (hp.existePlaca(placa, nombre)) {
                utilidades.mostrarAlerta(RegistroAuto.this, "", "La placa y nombre del vehiculo ya existe.");
                } else {
                Auto autosConsulta = LlamadoServicio(placa);
                if (autosConsulta != null) {
                    hp.nuevoAuto(autosConsulta);
                    // startActivity(new Intent(RegistroAuto.this, PrincipalAutos.class));
                    //   finish();
                    } else {
                    utilidades.mostrarAlerta(RegistroAuto.this, "", "Formato incorrecto de la placa");
                }
            }
            } else {
            utilidades.mostrarAlerta(RegistroAuto.this, "", "Formato incorrecto de la placa");
        }
    }
    //  progress.dismiss();
}

编辑

解决方案

我需要实现线程,而不是使用主线程

    new Thread(new Runnable() {@
Override
public void run() {
    try {
        // Here you should write your time consuming task...
        // Let the progress ring for 10 seconds...
        if (edtMiplaca.getText().toString().equalsIgnoreCase("")) {
            utilidades.mostrarAlerta(RegistroAuto.this, "", "Debe ingresar una placa");
        } else if (edtMiVehiculo.getText().toString().equalsIgnoreCase("")) {
            utilidades.mostrarAlerta(RegistroAuto.this, "", "Debe ingresar un nombre");
        } else {
            String placa = edtMiplaca.getText().toString();
            String nombre = edtMiVehiculo.getText().toString();
            if (utilidades.validaPlaca(placa) && utilidades.validaSoloAlfanumericos(placa)) {
                if (hp.existeSoloPlaca(placa)) {
                    utilidades.mostrarAlerta(RegistroAuto.this, "AutoPue", "La placa ya existe.");
                } else if (hp.existePlaca(placa, nombre)) {
                    utilidades.mostrarAlerta(RegistroAuto.this, "AutoPue", "La placa y nombre del vehiculo ya existe.");
                } else {
                    Auto autosConsulta = LlamadoServicio(placa);
                    if (autosConsulta != null) {
                        hp.nuevoAuto(autosConsulta);
                        startActivity(new Intent(RegistroAuto.this, PrincipalAutos.class));
                        finish();
                    } else {
                        utilidades.mostrarAlerta(RegistroAuto.this, "", "Formato incorrecto de la placa");
                    }
                }
            } else {
                utilidades.mostrarAlerta(RegistroAuto.this, "", "Formato incorrecto de la placa");
            }
        }
        progress.dismiss();
        Thread.sleep(10000);
    } catch (Exception e) {}
    //    progress.dismiss();
}
}).start();

我相信这是关于线程的。尝试在后台进行注册过程并在主线程上显示对话框。

您正在主线程中执行您的进程。相反,在主线程中显示 progressDialog。然后启动一个线程并在该线程中执行您的过程。在线程中,处理后隐藏progressDialog。