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?.
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
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?
pDialog = new ProgressDialog(AddStudentActivity.this) ;
change to
pDialog = new ProgressDialog(context) ;
its work.
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);
Related
I have a static AsyncTask in my MainActivity. I run this asynctask to download data from a URL. I'm trying to show a progress dialog from the async task but it crashes on this line: private ProgressDialog dialog = new ProgressDialog(mContext);
I pass the context through to the AsyncTask.
Here is my code:
private static class MyTasksParse extends AsyncTask<String, String, JSONObject> {
private Context mContext;
private ProgressDialog dialog = new ProgressDialog(mContext);
public MyTasksParse(Context context) {
mContext = context;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
this.dialog.setCanceledOnTouchOutside(false);
this.dialog.setMessage("Downloading Files... Please Wait...");
this.dialog.show();
}
#Override
protected JSONObject doInBackground(String... args) {
//get the url and parse it
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
I have tried casting mContext as an Activity as follows:
private ProgressDialog dialog = new ProgressDialog(mContext); but still the same error.
I'm not sure what I'm doing wrong. If anyone can help out, it would be great! Thanks :)
Since you have NullPointerException beacuse the variable mContext is null at this point:
private static class MyTasksParse extends AsyncTask<String, String, JSONObject> {
private Context mContext;
private ProgressDialog dialog = new ProgressDialog(mContext);
...
...
this will help:
private static class MyTasksParse extends AsyncTask<String, String, JSONObject> {
private Context mContext;
private ProgressDialog dialog;
public MyTasksParse(Context context) {
mContext = context;
dialog = new ProgressDialog(mContext)
}
When you receive the value of context in the constructor then you can instatiate correctly the ProgressDialog =).
mContext is null when you pass it to the constructor of ProgressDialog, thus throwing a NullPointerException.
This should work:
private Context mContext;
private ProgressDialog dialog;
public MyTasksParse(Context context) {
mContext = context;
dialog = new ProgressDialog(mContext);
}
The mContext is null when you initialise the ProgressDialog. That's why the crash is happening.
You need to change:
private ProgressDialog dialog = new ProgressDialog(mContext);
to:
private ProgressDialog dialog;
And initialise the progress dialog in onPreExecute:
dialog = new ProgressDialog(mContext);
You are getting NullPointerException beacuse mContext is null.
You need to init ProgressDialog inside constructor like
public MyTasksParse(Context context) {
mContext = context;
dialog = new ProgressDialog(mContext)
}
I am trying to show a ProgressDialog within a Fragment class. The following code just works within an Activity class but not for Fragment. Can somebody please help me on this, why this ProgressDialog implementaion just works within an Activity and not for a Fragment?
private class ProcessUpdateProfile extends
AsyncTask<String, String, JSONObject> {
private ProgressDialog nDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
nDialog = new ProgressDialog(PFragment.this); //Here I get an error: The constructor ProgressDialog(PFragment) is undefined
nDialog.setMessage("Loading..");
nDialog.setTitle("Checking Network");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
}
Try this in Fragment
nDialog = new ProgressDialog(getActivity());
ProgressDialog take Context input so use getActivity() in object creation.
ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);
I am running an Async(doInBackground) task in android.
I need to populate a progress bar for the task. So i am showing a progressDialog in onPreExecute,
The signature of ProgressDialog.show is Show(Context,Title,message)
But what would be the Context here?
#Override
protected void onPreExecute()
{
progress = ProgressDialog.show(???, "Loading", "Please Wait");
}
Create a constructor for your AsyncTask that takes a context as a parameter.
public class async extends AsyncTask<String, Integer, String>{
private Context context;
public async(Context context) {
this.context = context;
}
#Override
protected void onPreExecute() {
// Manipulate progress bar
}
Then use this to execute it
async mTask = new async(context).execute(params);
Context can be only of Activity ,Service or Brodcast not of any other class like Asyncktask.So put the Context of that Activity where you are using that AsyncTask class.
You can pass the activity context in the AsyncTask constructor to create the ProgressDialog :
MyAsyncTask constructor :
public MyAsyncTask(Context context){
progressDialog = new ProgressDialog(context, "Loading", "Please wait...");
}
onPreExecute method :
#Override
protected void onPreExecute()
{
progressDialog.show();
}
or store the context and create the dialog in the onPreExecute methods (but I prefer use the first way) :
public class MyAsyncTask extends AsyncTask{
private Context mContext;
public MyAsyncTask(Context context){
this.mContext = context;
}
#Override
protected void onPreExecute()
{
progress = ProgressDialog.show(this.mContext, "Loading", "Please Wait");
}
}
And in activity when you declare MyAsyncTask, you pass the activity:
MyAsyncTask asyncTask = new AsyncTask(this);
asynchTask.execute();
Add this function in your class
private Context getDialogContext() {
Context context;
if (getParent() != null)
context = getParent();
else
context = this;
return context;
}
In your asynctask use it as follows
#Override
protected void onPreExecute()
{
progress = ProgressDialog.show(getDialogContext(), "Loading", "Please Wait");
}
if you want to use only this as context then your Asynctask should be written as an inner class of a class which extends the Activity class. Then your context is the name of the class which extends Activity. Still it is better practice to pass the context like this:
ClassExtendingActivity.this
you can pass current activity view reference like MainActivity.this
Document doc = new Obtainer(context, uri).execute().get();
This code in the activity class renders the Obtainer(which extends AsyncTask) which gets the xml document from the url. This is the onPreExecute method:
protected void onPreExecute() {
super.onPreExecute();
System.out.println("Pre execute began");
exception = null;
dialog = new ProgressDialog(context);
dialog.setMessage("Loading started");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
System.out.println("Preexecute end");
dialog.show();
}
context is set in the Constructor:
public Obtainer(Context c, String addr) {
context = c;
address = addr;
}
During the runtime I can see in the console output both "Pre execute began" and "Preexecute end" but the progress dialog is not shown. What is the probleM?
Use this code, it works for me:
class Obtainer extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(App.this); // App - your main activity class
dialog.setMessage("Please, wait...");
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// ...
}
#Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}
}
And in your main activity class method call
new Obtainer().execute();
What Context are you passing when you create your Obtainer (AsyncTask subclass)?
If you are using the Application context via getApplicationContext(), it can not be used to create a Dialog (or any View for that matter). You need to pass it a Context that can create Views.
"If you're in the habit of using your application context (from a call to getApplicationContext(), for example) in places where you need a Context to create views, it's only a matter of time until you find a case where things don't work quite like you would want or expect."
From: https://plus.google.com/107708120842840792570/posts/VTeRBsAeyTi
I have been searching for an answer for this for some time now. I have an async task that downloads the database needed for my app, while this is downloading my app cant do anything as all the data it references is in this file, i have the app waiting for the file to be downloaded but i am attempting to show a progress dialog so the user knows something is happening while they wait for this to happen.
my code is currently
public class fileDownloader extends AsyncTask<Void, Integer, SQLiteDatabase>
{
private File dbFile;
private ProgressDialog progressDialog;
private Context context;
private SQLiteDatabase database;
private SQLiteDatabase.CursorFactory factory;
public fileDownloader(Context c)
{
super();
context = c;
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(this.context);
progressDialog.setMessage("Downloading Database...");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
#Override
protected SQLiteDatabase doInBackground(Void... v)
{
....
}
#Override
protected void onPostExecute(SQLiteDatabase db1)
{
progressDialog.dismiss();
}
however nothing shows up i have also tried directly calling ProgressDialog.show in the pre execute and moving this to the calling activity with no luck.
please help!
the solution to this was to look at the calling class the UI thread was getting blocked therefor the dialog never showed up.
Hmm - how long does your doInBackground method run? Maybe your dialog is shown, but the time is just too fast for the dialog to show up...
Below code is working fine, I am using it:
private class DownloadHomeSectionData extends
AsyncTask<Void, Void, Boolean> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(mainScreen);
progressDialog.setMessage(getResources()
.getString(R.string.loading));
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
#Override
protected Boolean doInBackground(Void... params) {
for (HomeButton homeBtn : appDesigner.getHomeButtons()) {
StorageManager.getInstance().downloadFileSyncronously(
homeBtn.getTab_logo_selected_image());
StorageManager.getInstance().downloadFileSyncronously(
homeBtn.getTab_logo_unselected_image());
}
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
}
In your case, I think you may be passing wrong context to ProgressDialog.