活动已泄露窗口 - 自定义对话框

时间:2016-07-27 10:08:38

标签: android memory-leaks dialog

我正在尝试在Activity中显示自定义对话框,并发生此错误。我知道为什么herehere发生此错误,并且知道它的解决方案。但问题是我无法在代码中找到问题。我花了很多时间找不到运气的错误。 我点击按钮点击下面的方法

public void changePassword(View view){
    DialogChangePassword dialogChangePassword = new DialogChangePassword(ActivityMyAccount.this);
    dialogChangePassword.show();
    Window window = dialogChangePassword.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}

我在显示Activity生活时的对话框。我的问题;

  1. 我不能在这里驳回对话,我在Dialog课程内解雇。
  2. 我没有完成活动。如果由于活动结束而发生错误,为什么要完成?
  3. 如果错误不是因为完成活动,那么为什么会发生此错误?
  4. 我有一个NetworkOperation要在Dialog类中执行,它是否必须对此做些什么?
  5. 这是我的Dialog类

    public class DialogChangePassword extends Dialog {
        Context context;
        Button confirm, cancel;
    
        CustomEditText edtOldPwd, edtNewPwd, edtConfirmNewPwd;
    
        ProgressDialog progressDialog;
    
        String PASSWORD_INCORRECT_MESSAGE = "Old password is incorrect.";
    
        public DialogChangePassword(Context context) {
            super(context);
            this.context = context;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.dialog_change_password);
    
            if(progressDialog != null)
                progressDialog = null;
    
            progressDialog = new ProgressDialog(context);
            progressDialog.setTitle("Please wait...");
            progressDialog.setMessage("Updating Password.");
            progressDialog.show();
    
    
            confirm = (Button) findViewById(R.id.btnChangePassword);
            cancel = (Button) findViewById(R.id.btnCancel);
    
            edtOldPwd = (CustomEditText) findViewById(R.id.edtOldPassword);
            edtNewPwd = (CustomEditText) findViewById(R.id.edtNewPassword);
            edtConfirmNewPwd = (CustomEditText) findViewById(R.id.edtConfirmNewPassword);
    
            edtConfirmNewPwd.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    if (!edtNewPwd.getText().toString().equals(edtConfirmNewPwd.getText().toString()))
                        edtConfirmNewPwd.setError("Password doesn't match");
                }
            });
    
            confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!edtNewPwd.getText().toString().equals(edtConfirmNewPwd.getText().toString())) {
                        Toast.makeText(context, "Password doesn't match",         Toast.LENGTH_SHORT).show();
                        progressDialog.cancel();
                        return;
                    }
                    HashMap<String, String> params = new HashMap<>();
                    params.put("email", Utility.classUser.getEmailId());
                    params.put("password", edtOldPwd.getText().toString());
                    params.put("newPassword", edtNewPwd.getText().toString());
                    new NetworkOperation().execute(params);
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    
        private class NetworkOperation extends AsyncTask<HashMap<String,String>, Void, String> {
            @Override
            protected String doInBackground(HashMap<String, String>... params) {
                try {
                    ClassAPIRoutes routes = ClassAPIRoutes.initializeRoutes();
                    return routes.editUserPassword(params[0]);
                } catch (final IOException e) {
                    e.printStackTrace();
                    getOwnerActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    });
                    return "error";
                }
            }
    
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                progressDialog.cancel();
                dismiss();
                if(!s.equals("error")){
                    JSONObject jsonObject;
                    try {
                        jsonObject = new JSONObject(s);
                        if (jsonObject.get("success").toString().equals("true")) {
                            Toast.makeText(context, "Password successfully changed.", Toast.LENGTH_SHORT).show();
                            dismiss();
                        } else if (jsonObject.get("success").toString().equals("false")) {
                            if (jsonObject.get("message").toString().equals(PASSWORD_INCORRECT_MESSAGE)) {
                                Toast.makeText(context, PASSWORD_INCORRECT_MESSAGE, Toast.LENGTH_SHORT).show();
                                resetFields();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getContext(), s, Toast.LENGTH_SHORT).show();
                    }
                }
    
            }
        }
    
        private void resetFields(){
            edtOldPwd.setText("");
            edtNewPwd.setText("");
            edtConfirmNewPwd.setText("");
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

我已从onCreate(...)移除了显示进度对话框,并将其放在Async内的OnPreExecute(...)课程中,并且工作正常

相关问题