无法在Fragment中的postexecute方法中关闭进度对话框

时间:2014-01-09 11:46:07

标签: android android-asynctask

未调用Asynctask中的

onPostExecute方法,并且不会解除progressdialog。我也没有任何例外。任何帮助将不胜感激  这是我的代码

 public class ParseAsync extends AsyncTask<String, Void, Void> 
{
    ProgressDialog pDialog = null;
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        pDialog = ProgressDialog.show(getActivity(), "", "Loading.....", true, false);
    }

    @Override
    protected Void doInBackground(String... params) 
    {
        RSSFeedXMLParser parser = new RSSFeedXMLParser();
        String urls = params[0]; 
        parser.parsedata(urls);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) 
    {
         pDialog.dismiss();
         listAdapter = new ListAdapter(getActivity());
         lvnewsfeed.setAdapter(listAdapter); 
    }   

}

我在oncreateview方法中调用asynctask,如下所示:

new ParseAsync().execute("rssfeedurl");

4 个答案:

答案 0 :(得分:0)

嗨,请有一行

dialog = new ProgressDialog(context);

在异步任务中添加这些行

public ParseAsync (YoursActivity activity) {
        this.activity = activity;
        dialog = new ProgressDialog(activity );
    }

答案 1 :(得分:0)

尝试将此添加到您的班级......

ParseAsync(String urlPass) {
            url = urlPass;
        }

并像这样启动你的AsyncTask ..

new ParseAsync(urlTopass).execute();

对于Ex:

private class connectAsyncTask extends AsyncTask<Void, Void, String> {
        private ProgressDialog progressDialog;
        String url;

        connectAsyncTask(String urlPass) {
            url = urlPass;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage("Fetching route, Please wait...");
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }

        @Override
        protected String doInBackground(Void... params) {
            JSONParser jParser = new JSONParser();
            String json = jParser.getJSONFromUrl(url);
            return json;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            progressDialog.hide();
            if (result != null) {
                drawPath(result);
            }
        }
    }

答案 2 :(得分:0)

制作 ParseAsync 类的构造函数

Context ctx;

public ParseAsync (Context mContext) {
    this.ctx = mContext;
}

现在将此 ctx 变量传递给ProgressDialog一个Context参数。

@Override
protected void onPreExecute() 
{
    super.onPreExecute();
    pDialog = ProgressDialog.show(ctx, "", "Loading.....", true, false);
}

答案 3 :(得分:-1)

您需要一个返回类型才能调用onPostExecute()。您可以将Void返回类型更改为String并返回一个简单的“done”字符串。那样onPostExecute()就会被调用。