1

So, I have my AsyncTask as below:

private class AsyncRetriever extends AsyncTask<IotdHandler,Void,IotdHandler>{
        ProgressDialog pDialog;
        @Override
        protected void onPreExecute(){
            pDialog = new ProgressDialog(getApplicationContext());
            pDialog.setMessage(getResources().getString(R.string.toast_msg1));
            pDialog.show();
        }
//------------------------------------------------------------------------------  

This is an inner class of the MainActivity. However, LogCat flags pDialog.show() as an error.
It says, Unable to start activity ComponentInfo.

How do I solve this?

An SO User
  • 24,612
  • 35
  • 133
  • 221

2 Answers2

3

A ProgressDialog needs an Activity Context. Try this:

pDialog = new ProgressDialog(YourActivity.this);
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • Tried, worked. Just for information `getApplicationContext()` should do the same as what u said, right ? – An SO User Jul 31 '13 at 07:55
  • @LittleChild check this for details http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – Raghunandan Jul 31 '13 at 07:57
  • I think this [post](http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context) makes this question clear. – Steve Benett Jul 31 '13 at 07:57
1
private class AsyncRetriever extends AsyncTask<IotdHandler,Void,IotdHandler>{
    ProgressDialog pDialog;
Context mcontext;
public AsyncRetriever(Context c){
   mcontext=c
}
    @Override
    protected void onPreExecute(){
        pDialog = new ProgressDialog(mcontext);
        pDialog.setMessage(getResources().getString(R.string.toast_msg1));
        pDialog.show();
    }

pass the context of calling class in this constructor

vs.thaakur
  • 619
  • 3
  • 13