进度对话框未在异步任务中停止

时间:2016-06-07 05:49:46

标签: android progressdialog

我想关闭onPostexecute方法的进度对话框,但它没有关闭。我不知道为什么。我创建了很多正常工作的asyntask但我不知道为什么这不起作用。我尝试打印一些日志,它将在onPostexecute中正确显示,并且没有任何异常发生。我的数据正确存在并存储在sqlite中database.i没有使用execute.get()方法。下面是我试过的一些代码。

public class AsynctaskProof extends AsyncTask<String, Void, ArrayList<ProofDetailsGeSe>> 
{

    proofInterface call;
    Context context;
    String resString, stcode, resStr,proofIdFetch,proofNameFetch,proofIdTag,proofNameTag,methodName,envelope;
    BaseActivity ba;
    Object objectType;
    JSONObject object,jsonObject;
    DatabaseHelper db;
    public ProofDetailsGeSe proofgese;
    public static ArrayList<ProofDetailsGeSe> proofArray;

    public AsynctaskProof(Context c, proofInterface callback, String tag1, String tag2) {
        context = c;
        proofIdTag =tag1;
        proofNameTag =tag2;
        call = callback;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ba = new BaseActivity();
        db = new DatabaseHelper(context);
        proofgese = new ProofDetailsGeSe();
        proofArray = new ArrayList<ProofDetailsGeSe>();
        ba.showProgressDialog(context);
    }

    @Override
    protected void onPostExecute(ArrayList<ProofDetailsGeSe> proofDetailsGeSes) {
        super.onPostExecute(proofDetailsGeSes);
        Log.d("come2 in post1","dfvds");

        ba.closeProgressDialog();
        call.run(proofDetailsGeSes);
        Log.d("comes in post2","dfvds");
    }
    @Override
    protected ArrayList<ProofDetailsGeSe> doInBackground(String... params) {

        methodName = params[0];
        resStr = sRequestClass.getProof();

        envelope = ba.soapRequestdata(resStr, methodName);
        resString = ba.sendSoapData(context,envelope);

        try {
            jsonObject = new JSONObject(resString.substring(resString.indexOf("{"), resString.lastIndexOf("}") + 1));
            Log.d("jsonObject  group2", "" + jsonObject);
            object = jsonObject.getJSONObject("MRRESP");
            String  stcode1 = object.getString("STCODE");

            ResponseString.setStcode(stcode1);
            if (stcode1.equals("0")) {
                db.deleteData(DatabaseHelper.sqtable_ProofDetails);

                proofgese = new ProofDetailsGeSe();

                proofgese.setProofId(0);
                proofgese.setProofName("Select");

                proofArray.add(proofgese);

                JSONArray stmsg = object.getJSONArray("STMSG");

                for (int i = 0; i < stmsg.length(); i++) {

                    JSONObject detail = stmsg.getJSONObject(i);

                    proofIdFetch = detail.getString(proofIdTag);
                    proofNameFetch = detail.getString(proofNameTag);

                    proofgese = new ProofDetailsGeSe();

                    proofgese.setProofId(Integer.parseInt(proofIdFetch));
                    proofgese.setProofName(proofNameFetch);

                    db.saveProof(DatabaseHelper.sqtable_ProofDetails,Integer.parseInt(proofIdFetch),proofNameFetch);

                    proofArray.add(proofgese);
                }
            }
            else
            {
                BaseActivity.sMsg = object.getString("STMSG");
                Log.d("error " ,""+ BaseActivity.sMsg);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return proofArray;
    }
}

下面是创建对话框并显示它的功能

    public void showProgressDialog(Context c) {
            pleaseWaitDialog = new ProgressDialog(c);
pleaseWaitDialog.setMessage(c.getResources().getString(R.string.waitMsg));
        pleaseWaitDialog.setTitle(R.string.app_name);
        pleaseWaitDialog.setIndeterminate(true);
        pleaseWaitDialog.setIcon(R.drawable.progress);
        pleaseWaitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pleaseWaitDialog.setCancelable(false);
        pleaseWaitDialog.show();
    }

下面是我用于关闭对话框的功能

public static void closeProgressDialog() {
    if ((pleaseWaitDialog != null) && pleaseWaitDialog.isShowing()) {
        pleaseWaitDialog.dismiss();
        pleaseWaitDialog = null;
    }

}

1 个答案:

答案 0 :(得分:1)

请更改您的代码

public class AsynctaskProof extends AsyncTask<String, Void, ArrayList<ProofDetailsGeSe>> 
{

proofInterface call;
Context context;
String resString, stcode, resStr,proofIdFetch,proofNameFetch,proofIdTag,proofNameTag,methodName,envelope;
BaseActivity ba;
Object objectType;
JSONObject object,jsonObject;
DatabaseHelper db;
public ProofDetailsGeSe proofgese;
public static ArrayList<ProofDetailsGeSe> proofArray;

public AsynctaskProof(Context c, proofInterface callback, String tag1, String tag2) {
    context = c;
    proofIdTag =tag1;
    proofNameTag =tag2;
    call = callback;
}
@Override
protected void onPreExecute() {
    super.onPreExecute();
    ba = new BaseActivity();
    db = new DatabaseHelper(context);
    proofgese = new ProofDetailsGeSe();
    proofArray = new ArrayList<ProofDetailsGeSe>();

    // Showing progress dialog
    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(false);
    pDialog.show();
}

@Override
protected ArrayList<ProofDetailsGeSe> doInBackground(String... params) {

    methodName = params[0];
    resStr = sRequestClass.getProof();

    envelope = ba.soapRequestdata(resStr, methodName);
    resString = ba.sendSoapData(context,envelope);

    try {
        jsonObject = new JSONObject(resString.substring(resString.indexOf("{"), resString.lastIndexOf("}") + 1));
        Log.d("jsonObject  group2", "" + jsonObject);
        object = jsonObject.getJSONObject("MRRESP");
        String  stcode1 = object.getString("STCODE");

        ResponseString.setStcode(stcode1);
        if (stcode1.equals("0")) {
            db.deleteData(DatabaseHelper.sqtable_ProofDetails);

            proofgese = new ProofDetailsGeSe();

            proofgese.setProofId(0);
            proofgese.setProofName("Select");

            proofArray.add(proofgese);

            JSONArray stmsg = object.getJSONArray("STMSG");

            for (int i = 0; i < stmsg.length(); i++) {

                JSONObject detail = stmsg.getJSONObject(i);

                proofIdFetch = detail.getString(proofIdTag);
                proofNameFetch = detail.getString(proofNameTag);

                proofgese = new ProofDetailsGeSe();

                proofgese.setProofId(Integer.parseInt(proofIdFetch));
                proofgese.setProofName(proofNameFetch);

                db.saveProof(DatabaseHelper.sqtable_ProofDetails,Integer.parseInt(proofIdFetch),proofNameFetch);

                proofArray.add(proofgese);
            }
        }
        else
        {
            BaseActivity.sMsg = object.getString("STMSG");
            Log.d("error " ,""+ BaseActivity.sMsg);
        }
    }catch (Exception e) {
        e.printStackTrace();
    }

    return proofArray;
   }

  @Override
protected void onPostExecute(ArrayList<ProofDetailsGeSe> proofDetailsGeSes) {
    super.onPostExecute(proofDetailsGeSes);
    Log.d("come2 in post1","dfvds");

    // Dismiss the progress dialog
    if (pDialog.isShowing()){
            pDialog.dismiss();
    }

    call.run(proofDetailsGeSes);
    Log.d("comes in post2","dfvds");
  }
}
相关问题