I have created an AsyncTask and I set it as static like this
public static class CreateLiveEventTask extends AsyncTask<Void, Void, List<EventData>>
Now I want to add a ProgressDialog to onPreExecute() , so I write some code like this
progressDialog = ProgressDialog.show(this, null,
getResources().getText(R.string.loadingEvents), true);
But it results in error and said that I need to change AsyncTask to not static . I need this AsyncTask at the other Activity , how can I do to solve this ?
Can not create outer class as Static in java
Why can't we have static outer classes
This is reference link
Change AsyncTask to non static, & your can create an object of the class containing "CreateLiveEventTask" in the other activity to access CreateLiveEventTask.
Hope it'll help
Related
textview and button in my listview by using adapter class. When i click on that button i have to call AsyncTask passing parameters i.e String of that perticular position in adapter class getview method .Here am created my Asynctask is another class i.e an activity class. Please provide some examples.
Thanks in advance.
Your AsycnTask takes an Array of some sort - Strings for instance, so when you instantiate the AsyncTask, you just pass it an Array like so:
String[] arr = new String[] {"A string to pass..."};
MyAsyncTask task = new MyAsyncTask();
task.execute(arr);
Full example on how to use it:
http://www.android-ever.com/2012/10/android-asynctask-example.html
Create public class in different file or within your activity. If you are creating within Activity then define like this way
public class static MyAsync extends AsyncTask<String, Void, String>{
}
and then used anywhere like this
YourActivity.MyAsync myAsync = new YourActivity.MyAsync();
for passing the value to the your Async class use this way
myAsync.execute(yourstring);
access in doInBackground like this way
public String doInBackground(String... param){
String s = param[0]; // here you can access you string like this way
}
I have an outer class method that executes an inner class extending AsyncTask, and I want that outer method to then sleep till the AsycnTask tells it to continue. Basically, I am pulling something from a DB in the inner class, and calling it from the outer class, however I need the outer class to wait till it has actually been retrieved before it tires to access it.
public class A{
String response;
public String returnResponse{
new B().execute();
// wait for signal from B
return response;
}
private class B extends AsyncTask<String, Void, Void>{
response = string pulled from online db;
//once response has been set, signal A.returnResponce to stop waiting
}
Any ideas?
Thanks,
use
new B().execute().get();
instead of
new B().execute();
for make waiting until AsyncTask execution complete
use this
new B().execute().get();
instead of your code
new B().execute();
this will wait for signal from B.
I'm learning from Google's Android developing tutorial and i came across a problem.
In Android's Connecting to the Network Guide it says to create a class that extends AsyncTask.
So when I wrote the class it automatically implements the method as follows:
private Object doInBackground(Object... args) {..} //it's fine
but when i try writing just as it says in the tutorial:
private String doInBackground(String... args) {..} //it gives an error
and the error says:
The method doInBackground(String...) of type MainActivity.DownloadWebpageText must override a superclass method.
So how do I change Object to String without getting an error there?
When you extend AsyncTask you must define the inputs to the background, progress, and post execute methods. Like this
private class MyTask extends AsyncTask<String, Void, Boolean>
Which would define a class that extends AsyncTask and takes as input String and returns a Boolean to the onPostExecute method.
I’m using AsyncTask to load bitmaps in the background. I created a class Mybackground that inherits from AsyncTask.
If I do the following it works,
new MyBackground().execute();
But when I call it this way,
MyBackground mBackground=new MyBackground();
mBackground.p1=1;
mBackground.p2="tets";
MyBackground.execute();
I get the error cannot make a static reference to a non static.
Why am I getting this error. Is there a way to do this? If not what would be a good way to pass in 2 different arguments, since execute only takes in 1 parameter?
You would call mBackground.execute() which calls the execute method of a particular instance of your MyBackground class.
Calling new MyBackground().execute() creates a new MyBackground instance and then calls execute on that, which is why you don't get the error.
Take a look at Java: static and instance methods if you are unsure on what the difference between a static and instance method is.
Static methods do not require an instance of that type to exist, and static properties are shared between all instances of a class.
If you want to pass some data into your AsyncTask you can create a new constructor for it:
public class MyBackground extends AsyncTask<Void, Void, Void> {
...
private int p1;
private String p2;
public MyBackground(int p1, String p2) {
this.p1 = p1;
this.p2 = p2;
}
...
}
You would then use either:
MyBackground myBackground = new MyBackground(1,"tets");
myBackground.execute();
// or
MyBackground myBackground = new MyBackground(1,"tets").execute();
Joseph Earl has a good explanation of why you are getting the static reference error.
I've been using my Activity class to access my DB which made my program freeze sometimes.
So I decided to use AsyncTask instead to handle the DB.
My problem is I don't know how to instantiate my SQLite DB "TheDB" from AsyncTask's class
public class myClass extends AsyncTask<Void, Void, Void>{
private TheDB db;
any method() {
this.db = new TheDB(this); //<-- Error here
}
this worked fine on the Activity class, but it I dont know how to use it here
TheDB's constructor is TheDB(Context context) but this class is not a "context" so how can i use my DB here?
please provide examples if you can
and please do not give me links to google's references, am a newbie and i find them hard to follow
you need to pass the application context here
this.db = new TheDB(getApplicationContext());
import android.content.Context;
public class SuperTask extends AsyncTask<Void, Void, Void> {
private final Context mContext;
public SuperTask(Context context) {
super();
this.mContext = context
}
protected Void doInBackground(Void... params) {
// using this.mContext
}
}
public class MainActivity extends Activity {
// and run from Activity
public void onButtonClick(View view) {
new SuperTask(this.getApplicationContext()).execute();
}
}
There are two ways that i see:
Pass a context object to your AsyncTask constructor, then instantiate database like this this.db = new TheDB(context);
Or you probably can pass the actual database object to the constructor, but the first approach seems better.
An important part of learning to program is learning to read and understand documentation. As documentation goes, the Android docs are pretty detailed, so its really worth your time to understand how they work.
As you can see in the AsyncTask docs, there is no onCreate or onExecute method in an AsyncTask.
The docs clearly walk you through the 4 main functions of an AsyncTask, onPreExecute(), doInBackground(Params...), onProgressUpdate(Progress...), onPostExecute(Result).
The likely choices for your instance are onPreExecute() or doInBackground(Params...). The difference is whether or not you want the initializition to occur on the UI thread. If you don't, then do it in doInBackground(Params...).