窗口在显示进度对话框时泄露在异步任务中

时间:2016-09-21 07:41:09

标签: android android-asynctask window progressdialog

我有一个AsyncTask我在异步任务的preExecute()方法中显示进度对话框,并在异步任务的postExecute()方法中将其解除。

我也在检查对话框是否为空。同时将setCancelable设置为false以进入对话框。试过在SO上给出的每一个解决方案,但仍然是窗口泄露。

异步任务:

 public class RegisterUserAsyncTask extends AsyncTask<String, Void, JSONObject> {
    String api;
    JSONObject jsonParams;
    String muserName;
    String mfullName;
    String mpassword;
    String mmobileNo;
    String memailId;
    String mdeviceId;
    File mprofileImage;
    private ProgressDialog progressDialog = null;

    private static String KEY_SUCCESS = "Success";
    private Context mContext;

    public RegisterUserAsyncTask(Context context, String fullName, String userName, String password, String mobileNo, String emailId, String deviceId, File profileImage) {
        this.mContext = context;
        this.muserName = userName;
        this.mpassword = password;
        this.mfullName = fullName;
        this.mmobileNo = mobileNo;
        this.memailId = emailId;
        this.mdeviceId = deviceId;
        this.mprofileImage = profileImage;

    }
    @Override
    protected void onPreExecute(){
        super.onPreExecute();

        if(progressDialog == null) {
            progressDialog = new ProgressDialog(mContext);
            progressDialog.setMessage("Creating Account...");
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
        else {
            progressDialog.dismiss();
        }
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        try {

                   //Url
            api = ServiceUrl.getBaseUrl() + ServiceUrl.getregister();
            //Build JsonObject
            jsonParams = new JSONObject();
            String userName = this.muserName;  // params[0] is username
            String fullName = this.mfullName;  // params[1] is fullname
            String password = this.mpassword;  // params[2] is password
            String mobileNo = this.mmobileNo;  // params[3] is mobile
            String emailId = this.memailId;    // params[4] is emailid
            String deviceId = this.mdeviceId;  // params[5] is deviceid

            jsonParams.put("full_name", fullName);
            jsonParams.put("user_name", userName);
            jsonParams.put("password", password);
            jsonParams.put("mobile_no", mobileNo);
            jsonParams.put("email_id", emailId);
            jsonParams.put("device_id", deviceId);

            try {
                if(convertFileToString(this.mprofileImage)!=null) {
                    jsonParams.put("profile_image", convertFileToString(this.mprofileImage));

                    System.out.println("convertFileToString(profile_image)" + convertFileToString(this.mprofileImage));
                }
                else
                { jsonParams.put("profile_image", " null");}

            } catch (Exception e) {

                System.out.println("convertFileToString(profile_image)");

            }

            ServerRequest request = new ServerRequest(api, jsonParams);
            return request.sendRequest();

        } catch (JSONException je) {
            return Excpetion2JSON.getJSON(je);
        }
    }  //end of doInBackground

    @Override
    protected void onPostExecute(JSONObject response) {
        super.onPostExecute(response);


        if (response.has("message")) {
            String message = null;
            try {
                if (response.getString("message").equalsIgnoreCase(KEY_SUCCESS)) {
                    Toast.makeText(mContext, "success", Toast.LENGTH_LONG).show();
                    progressDialog.dismiss();
                    progressDialog = null;

                } else {
                    Toast.makeText(mContext, "Could not Register ", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(mContext, RegisterActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    progressDialog.dismiss();
                    progressDialog = null;
                    mContext.startActivity(intent);
                }
            } catch (JSONException e) {
                e.printStackTrace();

            }

        }
    }
}

在活动的onCreate()方法中按钮的onClicked方法中调用RegisterAsyncTask的活动。

    b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (checkValidation()) {

                    registerUser();

                } else
                    Toast.makeText(RegisterActivity.this, "Form contains error", Toast.LENGTH_LONG).show();

            }
        });
    }



 private void registerUser() {
    String userName = edtuserName.getText().toString();
    String fullName = edtfullName.getText().toString();
    String password = edtPassword.getText().toString();
    String confirm = edtconfirmPassword.getText().toString();
    String mobileNo = edtmobile.getText().toString();
    String emailId = edtemail.getText().toString();
    String deviceId = "233";

    new RegisterUserAsyncTask(RegisterActivity.this, fullName, userName, password, mobileNo, emailId, deviceId,mProfileImage).execute();

}

该怎么做?

请帮忙。谢谢..

1 个答案:

答案 0 :(得分:3)

由于“上下文”,可能会出现此问题。 “进度”对话框具有活动的上下文,但您可以在完成异步任务之前执行完成活动。所以请检查一次。