0

onPostExecute method in Asynctask is not being called and the progressdialog is not getting dismissed. I'm not getting any exceptions as well. Any help would be appreciated.
Here is my code

 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); 
    }   

}

I'm calling the asynctask in oncreateview method like this:

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

4 Answers4

0

hi please have a line

dialog = new ProgressDialog(context);

add these lines in async task

public ParseAsync (YoursActivity activity) {
        this.activity = activity;
        dialog = new ProgressDialog(activity );
    }
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
  • Im using fragment instead of activty and I tried this, but it doesn't work. public ParseAsync (Context context) { context = getActivity(); pDialog = new ProgressDialog(context); } –  Jan 09 '14 at 12:00
0

Try adding this to your class ...

ParseAsync(String urlPass) {
            url = urlPass;
        }

And initiate your AsyncTask like this ..

new ParseAsync(urlTopass).execute();

For 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);
            }
        }
    }
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
0

Make a Constructor of your ParseAsync class

Context ctx;

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

Now pass this ctx variable to ProgressDialog a Context parameter.

@Override
protected void onPreExecute() 
{
    super.onPreExecute();
    pDialog = ProgressDialog.show(ctx, "", "Loading.....", true, false);
}
Piyush
  • 18,895
  • 5
  • 32
  • 63
-1

You need a return type in order to get onPostExecute() to get called. You can change the Void return type to String and return a simple "done" String. That way onPostExecute() does get called.

Tim Kranen
  • 4,202
  • 4
  • 26
  • 49
  • @Raghunandan What Android version are you running? – Tim Kranen Jan 09 '14 at 11:56
  • @Raghunandan If you are pre-Jellybean take a look at this: The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN. Are you doing this manually if you're pre-Jellybean? – Tim Kranen Jan 09 '14 at 11:58
  • invoking asynctask in `onCreateView` is not on ui thread? Quoting op "I'm calling the asynctask in oncreateview method like this: `new ParseAsync().execute("rssfeedurl")`" – Raghunandan Jan 09 '14 at 11:59
  • Take a look at this thread, specifically the second answer: http://stackoverflow.com/questions/4494720/asynctask-onpostexecute-never-gets-called It explains the issue more elaborate. – Tim Kranen Jan 09 '14 at 12:01