I have a an activity with AsyncTask sub-classed. I lose all my variables once the async task is executed. I am stepping through my code in debug mode. As soon as “MyAsync().execute()” finishes the “formatedURL” variable (and all the others) have no values. before that, they have the correct values. Then, for some odd reason, they lose the values. Am i making a simple OO mistake or is garbage collection doing something i am not aware of.
public class NearbyList extends Activity {
double lat;
double lng;
String restName;
GPSHandling gps;
String formatedURL;
JSONObject jobject;
ArrayList<HashMap<String, String>> listOfHM;
ArrayList<String> listOfValues;
String currentName;
ListView lv;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearby_places_list);
context = getApplicationContext();
gps = new GPSHandling(this);
lat = gps.getMyLatitude();
lng = gps.getMyLongitude();
restName ="";
formatedURL = GooglePlacesStuff.placesURL(lat, lng, 16000, "food", restName, true); //make a proper url. next step is to get a JSON object from this.
new MyAsync().execute();// in order to run networking it must not be done in the UIthread. I use async task to take care of this in order to
//reduce the code of doing complex threading since this is a simple calculation
}
class MyAsync extends AsyncTask<Void, Integer, Boolean>{
@Override
protected Boolean doInBackground(Void... params) {
try {
jobject = GooglePlacesStuff.getTheJSON(formatedURL);
listOfHM = JSONextractor.getJSONHMArrayL(jobject);
// iterate through and get the names of the nearby restaurants from the array of hasmap strings
for(int i =0 ; i < listOfHM.size() ;i++ ){
currentName = listOfHM.get(i).get(JSONextractor.TAG_NAME);
listOfValues.add(currentName);
}
return true;
} catch (Exception e){
Log.e("Nearby List Activity", "exception", e);
return false;}
}
@Override
protected void onPostExecute(Boolean result){
super.onPostExecute(result);
if (result){
ListAdapter adapter = new SimpleAdapter(context, listOfHM, R.layout.nearby_places_list, new String[]{JSONextractor.TAG_NAME,
JSONextractor.TAG_VICINITY, JSONextractor.TAG_GEO_LOC_LAT}, new int[]{ R.id.name, R.id.vicinity, R.id.phone});
// adding data to listview
lv.setAdapter(adapter);
} else{
Toast.makeText(getApplicationContext(), "Need Internet & GPS access for this to work", Toast.LENGTH_LONG).show();
}
gps.stopUsingGPS(); // stop using the gps after i get the list to save on resource
}
}
}
Edit1:
looks like it is trying to run “super.onCreate(Bundle savedInstanceState)” multiple times in the doinbackground() method
Edit2: if i make the values static they don’t get lost. Its weird, even the variable “jobject” which is assigned inside the async task wont take an assignment unless its a static variable…. never seen anything like this
When you say they have no values, are you checking them inside the AsyncTask? If so, this might be the reason (from AsyncTask):
Basically, you shouldn’t access your instance variables from doInBackground() because it’s not thread-safe. Like the function says, it runs in a separate (background) thread. You can work around it by making them static (which you tried) or synchronize them, but it’s probably better to use AsyncTask the way it’s intended.
So I think you should do the following:
Pass in formatedURL as a parameter to the AsyncTask
return ArrayList<HashMap<String, String>> from doInBackground() (listOfHM)
Use the passed in ArrayList<HashMap<String, String>> in onPostExecute()
I would also additionally set the ListAdapter in onCreate, and just update the data backing the ListAdapter onPostExecute(). But I won’t discuss that here since it’s probably a separate question. This one is optional.
Code:
And in your onCreate() you would do
Hope it helps!