1
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?.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Yu Wil Son
  • 119
  • 2
  • 11

4 Answers4

2

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

Andrei T
  • 2,985
  • 3
  • 21
  • 28
1

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?

Community
  • 1
  • 1
Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
1

pDialog = new ProgressDialog(AddStudentActivity.this) ;

change to

pDialog = new ProgressDialog(context) ;

its work.

Yu Wil Son
  • 119
  • 2
  • 11
0

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);

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147