Yu Wil Son
Yu Wil Son

Reputation: 119

AsyncTask onPreExecute new progressdialog

class AddStudent extends AsyncTask<String, Void, ResultData> {

private ProgressDialog pDialog;

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(AddStudentActivity.this);
    pDialog.setMessage("Adding Product..");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}

private Context context;
  //CHANGE HERE....ADD PARAMATER

TextView tv_msg;
public AddStudent(Context context, TextView tv_msg) {
    this.context = context;
    this.tv_msg = tv_msg;

}

I have an error in (AddStudentActivity.this);

Error = com.blablablabla.AddStudentActivity is not an enclosing class.

What's the problem?.
How can I fix this?.

Upvotes: 1

Views: 433

Answers (4)

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

Error = com.blablablabla.AddStudentActivity is not an enclosing class.

above error comes whenever you try to use Activity context in other separate class . This should even harmful for you when it will give you memory leaks.

pDialog = new ProgressDialog(context);

Upvotes: 0

Andrei T
Andrei T

Reputation: 3083

If the asynctask is not a nested class of an activity you need to set/add the context as a parameter to the constructor.

 class AddStudent extends AsyncTask<String, Void, ResultData> {

    private ProgressDialog pDialog;
    private Context context;

    public AddStudent(Context context) {
       this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context) ;
        pDialog.setMessage("Adding Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
}

TextView it is part of the activity.
Or, if the asynctask is a nested class of an activity, then you can do what you want. More you can read in the example below:
ProgressDialog and AsyncTask

Upvotes: 2

Yu Wil Son
Yu Wil Son

Reputation: 119

pDialog = new ProgressDialog(AddStudentActivity.this) ;

change to

pDialog = new ProgressDialog(context) ;

its work.

Upvotes: 1

Ankur Aggarwal
Ankur Aggarwal

Reputation: 2220

You have not posted your entire code, so this is a bit of speculation, but here goes:

Most probably you have created a separate file for your AddStudent AsyncTask, or put it outside your AddStudentActivity class. You need to make AddStudent an inner class of AddStudentActivity to be able to use AddStudentActivity.this.

More info here : Android: AsyncTask recommendations: private class or public class?

Upvotes: 1

Related Questions