我有一個AsyncTask我在異步任務的preExecute()方法中顯示進度對話框,並在異步任務的postExecute()方法中解除它。在顯示進度對話框時漏出異步任務的窗口
我還檢查對話框是否爲空。還將setCancelable設置爲false,以顯示進度對話框。嘗試了所有解決方案,但仍然窗口泄漏。
異步任務:
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();
}
}
}
}
在活動呼叫RegisterAsyncTask在一個按鈕中的onCreate()的活性的方法onClicked方法。
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();
}
該怎麼辦?
請幫忙。謝謝..
'progressDialog.dismiss();'從'ifelse'外部''posstExecute'上有空檢查的條件 –
我也曾試過這種方法。仍然是窗戶泄漏。 @Dipalishah – Sid