如何在内部类片段中添加进度对话框

时间:2014-11-14 06:53:17

标签: android android-asynctask progressdialog

我想在片段中创建progressdialog,但是显示错误已经泄露了最初添加到此处的窗口com.android.internal.policy.impl.phonewindow。请帮忙,这是我的代码

 private class ListDaftarHargaAsync extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("retrieving...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            modelPost.setDataVouchers();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String result = modelPost.resultString;
        if (result.length() != 0) {
            progressDialog.dismiss();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        progressDialog.dismiss();
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                fillDaftarHarga();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

检查进度对话框是否显示 而你无法触及doInBackground中的UI线程

    @Override 
    protected void onPreExecute() { 
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("retrieving..."); 
        progressDialog.setIndeterminate(false); 
        progressDialog.setCancelable(false); 
        progressDialog.show();
    } 

    @Override 
    protected String doInBackground(String... params) {
        try { 
            modelPost.setDataVouchers(); 
        } catch (JSONException e) {
            e.printStackTrace();
        } 
        String result = modelPost.resultString;
        return null; 
    } 

    @Override 
    protected void onPostExecute(String result) {
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
        } 
        getActivity().runOnUiThread(new Runnable() {
            @Override 
            public void run() { 
                fillDaftarHarga(); 
            } 
        }); 
    } 
}

答案 1 :(得分:0)

leaked window com.android.internal.policy.impl.phonewindow that was originally added here.

以上错误显示是因为您在progressDialog.dismiss();上呼叫doInBackground