Reputation: 2924
I have MainActivity.java
Activity class which loads when application launches. I am using asyncTask
methods in sperate file named ServerOperation.java
. I need to show the progress bar in onPreExecute
method of asyncTask. I am using following code but its not working and application crashes:
public class ServerOperation extends AsyncTask<Void, Void, ArrayList<Job>> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
MainActivity myActivity = new MainActivity();
progressDialog = ProgressDialog.show(myActivity.getApplicationContext(),"PLEASE WAIT","LOADING JOBS...", true);
}
}
How should I make progressDialog work on main activity?
Thanks.
Upvotes: 1
Views: 2120
Reputation: 133560
You should never create an instance of Acitvity class. Its not a normal java class.
You can pass the Activity Context to the constructor of Asynctask
Invoke Aysnctask as
new ServerOperation(MainActivity.this).execute(params);
in MainActivity
Then
Context mContext;
public ServerOperation (Context context)
{
mContext =context;
}
Then
progressDialog = ProgressDialog.show(mContext,"PLEASE WAIT","LOADING JOBS...", true);
Upvotes: 3