I\'m a bit new with the AsyncTask and ProgressDialog and i\'m getting a null pointer exception error whenever i call new MyTask().execute();
on my button does my ap
Sounds like progress dialog is not getting initialized .So just make sure it is initialized correctly.
protected void onPreExecute() {
super.onPreExecute();
//initialize progress dialog here
progress.show();
}
EDIT :
class YourClass extends AsyncTask<...>
{
ProgressDialog progress;
Protected void onPreExecute(){
// create dialog here
progress = new ProgressDialog (...);
progress.setMessage(...);
progress.show();
}
protected void onPostExecute(...){
// dismiss dialog here
progress.dismiss();
}
}
nother part that I hope it helps ...if u want to change the text and percentage of progress during the background task u can define a handler
public class MyTask extends AsyncTask<Void, Integer, Void>
{
private ProgressDialog progressDialog;
private String mstepDescription;
protected int mprogressPercentage;
private String mstepDescription;
protected int mprogressPercentage;
Handler handle = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressDialog.setMessage(mstepDescription);
progressDialog.setProgress(mprogressPercentage);
}
};
void Step (String StepDescription, int Progress)
{
mstepDescription = StepDescription;
mprogressPercentage = Progress;
handle.sendMessage(handle.obtainMessage());
}
Protected void onPreExecute()
{
// create dialog here
progress = new ProgressDialog (...);
progress.setMessage(...);
progress.show();
}
protected void onPostExecute(...){
// dismiss dialog here
progress.dismiss();
}
@Override
protected Void doInBackground(Void... params)
{
// do somthing
Step("...", 40);
// do something else
}
}
use class MyTask constructor for passing Activity Context to initialize ProgressDialog and message which u want to show in ProgressDialog inside onPreExecute
as :
public class MyTask extends AsyncTask<Void, Integer, Void>{
Context context;
String str_mesg;
ProgressDialog progress;
public MyTask(Context context, String str_mesg){
this.context=context;
this.str_mesg=str_mesg;
}
@Override
protected void onPreExecute() {
//Show progress Dialog here
super.onPreExecute();
// create ProgressDialog here ...
progress = new ProgressDialog(context);
progress.setMessage(str_mesg);
// set other progressbar attributes
....
progress.show();
}
// your code here....
and start AsyncTask as on Button Click :
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new MyTask(Your_Current_Activity.this).execute();
}
});