I have a LocationMapActivity which used google map v2 to display locations of people (fetches locations from database according to person ID).I use async task which fetches location from database and add markers and routes on the map .
I re use the LocationMapActivity(Displays markers/route according to person ID) in two situations:
Click on Show all people om map Button-LocationMapActivity displays markers depicting each person.If the marker is clicked ,then the specific person's route is displayed by again sending an intent to LocationMapActivity with person ID as put extra.
Click on slide show map Button- LocationMapActivity firsts displays all markers depicting all people.Then after 10 secs it re renders map and display route of each person (one by one with 10sec interval). Refer accepted answer for my code : How to place Multiple MapViews v1 inside a ViewFlipper .
PROBLEM : Everything works perfectly in scenario 1. But In scenario 2 the ArraryList latLngArrlist losses value suddenly!.I have debugged the code the array list is never set to null it randomly gets null. These arrayList are private but not local variables.
Note: : I use a reset function to clear all the arraylists and non local variables before rendering the next map view .
My code is very lengthy so I'm posting only the relevant code below :
LocationMapActivity
public class LocationMapActivity extends Activity implements OnClickListener,
OnMarkerClickListener {
private ArrayList<String> latLngCheckinTimeList = null;//
private ArrayList<String> addressList = null;//
private ArrayList<LatLng> latLngArrList = new ArrayList<LatLng>();//
private ArrayList<Long> checkinTimeArrList = new ArrayList<Long>();//
private String selectedFieldOfficerId;
private GoogleMap googleMap;
private List<List<HashMap<String, String>>> route = null;
private HashMap<Marker, String> markerHmap = new HashMap<Marker, String>();//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity_screen);
context = this;
//person ID according to which will be displayed
selectedFieldOfficerId = getIntent().getStringExtra(
AllConstants.FOIdKey);
latLngCheckinTimeList = new ArrayList<String>();
//my code
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
mapTask = new MapTask(context, selectedFieldOfficerId, this);
mapTask.execute();// async task for url of route
}
googleMap.setOnMarkerClickListener(this);
}
}
MapTask async task
@Override
protected Void doInBackground(Void... params) {
helper = new EmployeeHelper(getApplicationContext());
if (!fieldOfficerId.equals(AllConstants.ShowAllFOInMap)
&& !fieldOfficerId.equals(AllConstants.ShowAllFOInMapSlide)) {
// map for specific fo
showSpecificFoMap(fieldOfficerId);
} else if (fieldOfficerId.equals(AllConstants.ShowAllFOInMap)) {
// show all emp in map
showAllFoOnMap((AllConstants.ShowAllFOInMap));
} else if (fieldOfficerId.equals(AllConstants.ShowAllFOInMapSlide)) {
// show slide show
//PROBLEM HERE
Log.i("map", "in slide show if ");
ArrayList<String> foIdList = new ArrayList<String>();
Boolean condition = true;
while (condition && !isFinishActivity) {
// runs only once
Log.i("map", "in slide show if WHILE");
mapScreenshotsNo = 1;
try {
foIdList = showAllFoOnMap((AllConstants.ShowAllFOInMapSlide));//ARRAYLIST BECOMES NULL IN THIS FUNCTION
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
if (progressDialog != null
&& progressDialog.isShowing()
&& !isFinishActivity)
progressDialog.dismiss();
}
});
try {
util.haveASleep(slideShowInterval);
} catch (Exception e) {
e.printStackTrace();
}
resetVariables();
mapScreenshotsNo++;
if (foIdList != null) {
try {
mapScreenshotsNo = showEachFOMap(foIdList,
mapScreenshotsNo);
} catch (Exception e) {
e.printStackTrace();
}
resetVariables();
}
condition = false;
}
if (mapTask != null)
mapTask.cancel(true);
// mapActivity.finish();
}
return null;
}
I understand Arraylists are loosing value. But I dont know how to save ArrayList type in persistent memory as it is not of primative type. I'm struggling with this problem since few days .
EDIT :ArrayList becomes null suddenly with any set pattern .Sometimes the code runs fine sometime it gives null pointer at arraylists. Usually the null pointer is caused by ArrayList latLngArrList
Please help !!!
Thanks.