活动已泄漏窗口(AlertDialog.Builder)

时间:2018-11-01 15:20:54

标签: android

我尝试了此错误的解决方案,例如将dismiss()放在onStop()onPause上,但仍然收到错误,我有一个Dialog登录应用

这是我为登录活动创建的on

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_lojista);

        connection = new DataBaseController(this);

        dialogLojista = new Dialog(this);

        dialogLojista.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogLojista.setContentView(R.layout.dialog_login_lojista);
        WindowManager.LayoutParams lp = dialogLojista.getWindow().getAttributes();
        lp.dimAmount = 0.9f; // Dim level. 0.0 - no dim, 1.0 - completely opaque
        dialogLojista.getWindow().setAttributes(lp);

        dialogLojista.setCancelable(false);
        dialogLojista.show();

        entrar = dialogLojista.findViewById(R.id.entrar);
        sair = dialogLojista.findViewById(R.id.sair);
        cnpjInput = dialogLojista.findViewById(R.id.CNPJ);
        passInput = dialogLojista.findViewById(R.id.password);
        cnpjInput.requestFocus();

        toast = Toast.makeText(this, "erro", Toast.LENGTH_SHORT);
    }

dialogLojista显示出来。.

因此用户单击进入,它将转到asynctask

public class LoginLoja extends AsyncTask<String, Void, Boolean> {
        private Handler mHandler = new Handler(Looper.getMainLooper());
        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mHandler.post(() -> {
                progressDialog = new ProgressDialog(context);
                progressDialog.setTitle("Buscando informações");
                progressDialog.setMessage("Por favor, aguarde a identificação...");
                progressDialog.setCancelable(false);
                progressDialog.show();
            });
        }

        @Override
        protected Boolean doInBackground(String... strings) {
           ...
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            mHandler.post(() -> progressDialog.dismiss());
        }
    }

onPostExecute progressDialog被关闭

如果有具有凭据的用户,请继续:

public void entrar(View view) {
        String pass = passInput.getText().toString();
        String cnpj = cnpjInput.getText().toString();
        if (pass.equals("") || cnpj.equals("")) {
            showToast("Verifique as Informações!");
        } else {
            DataBaseController.LoginLoja loginLoja = connection.new LoginLoja();
            try {
                if (loginLoja.execute(cnpj, pass).get()) {
                    dialogLojista.dismiss();
                    Intent intent = new Intent(this, MainActivity.class);
                    //intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent);
                } else {
                    showToast("Verifique as Informações!");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }

dialogLojista已被关闭

onCreate的MainActivity没有任何Dialog

用户单击以扫描二维码并签入,它遵循asynctask模式, onpre->创建progressDialog onpost-> dismiss()

泄漏发生在这里

@Override
    public void onBackPressed() {
        super.onBackPressed();
        builder = new AlertDialog.Builder(this);
        builder.setMessage("Deseja realmente deslogar-se?")
                .setTitle("Sair da Conta")
                .setPositiveButton("Ok", (dialog, id) -> {
                    Intent intent = new Intent(this, LoginLojista.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                        startActivity(intent);
                })
                .setNegativeButton("Cancelar", (dialog, id) -> {
                })
                .setCancelable(true);
        builder.create().show();
    }

builder.create.show()上。

builder尚未实例化,为什么会发生泄漏?

ps:在第一次活动中,dialogLojistaonStop不能被撤消,因为需要登录

0 个答案:

没有答案