gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:14:30 25 4
gpt4 key购买 nike

我有一个 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();

}

}
}
}

在 Activity 的 onCreate() 方法中,在按钮的 onClicked 方法中调用 RegisterAsyncTask 的 Activity 中。

    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();

}

为此要做什么?

请帮忙。谢谢..

最佳答案

这个问题可能是由于“上下文”引起的。进度对话框有一个 Activity 的上下文,但您可以在完成异步任务之前完成 Activity 。所以请检查一次。

关于android - 窗口在显示进度对话框时在异步任务中泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39610338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com