2016-09-21 2 views
1

私は1つのAsyncTaskを持っています非同期タスクのpreExecute()メソッドで非同期タスクのpostExecute()メソッドで進行状況ダイアログを表示しています。ウィンドウが非同期タスクで進行状況ダイアログを表示中にリークしました

また、ダイアログがNULLかどうかを確認しています。また、進行ダイアログには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(); 

} 

これはどうすればよいですか?

助けてください。ありがとう..

+0

'progressDialog.dismiss();'ヌルチェック付き 'ifelse'条件外で' posstExecute' –

+0

私もそのようにしました。まだ窓が漏れていた。 @Dipalishah – Sid

答えて

3

この問題は、「コンテキスト」によって発生する可能性があります。進行状況ダイアログにはアクティビティのコンテキストがありますが、非同期タスクを完了する前にアクティビティを終了することができます。だから一度それをチェックしてください。

+0

私はどこでも活動を終了していません。 RegisterAsyncTaskをアクティビティのonCreateで呼び出し、RegisterAsyncTask onPostExcecuteで結果が成功すれば、registerActivityを終了して新しいアクティビティを呼び出すだけです。@ Kailas Bhakade – Sid

関連する問題