An SO User
An SO User

Reputation: 24998

Android onPreExecute cannot show a ProgressDialog

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?

Upvotes: 1

Views: 2384

Answers (2)

vs.thaakur
vs.thaakur

Reputation: 619

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

Upvotes: 1

Steve Benett
Steve Benett

Reputation: 12933

A ProgressDialog needs an Activity Context. Try this:

pDialog = new ProgressDialog(YourActivity.this);

Upvotes: 3

Related Questions