2

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:

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

  2. 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.

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66

2 Answers2

0

try changing these variable value inits:

private ArrayList<String> latLngCheckinTimeList = null;

to just

private ArrayList<String> latLngCheckinTimeList;

And move variable creation to onCreate:

private  ArrayList<LatLng> latLngArrList = new ArrayList<LatLng>();//

onCreate..

latLngArrList = new ArrayList<LatLng>();
Hardy
  • 5,590
  • 2
  • 18
  • 27
-1

this is datacontroller class which is singleton class means The class which have only single object or single instance that we call singleton class.

     public class DataController {

            private static DataController ref;

            public static DataController getInstance()
            {
                if(ref==null)
                    ref = new DataController();

                return ref;
            }

            public void deleteInstance()
            {
                ref=null;
            }

    //here we can declare any type of data either it is primitive type or derived type data like this 

    public ArrayList<CommanWrapperForListing> arraylist_wrapper_datacntrlr = new ArrayList<CommanWrapperForListing>();

        public boolean iswifi = true; 

        public HashMap<String, ArrayList<CommanWrapperForListing>>caching = new HashMap<String,    ArrayList<CommanWrapperForListing>>();
        public HashMap<String, HashMap<String, ArrayList<CommanWrapperForListing>>>caching_machine = new HashMap<String, HashMap<String, ArrayList<CommanWrapperForListing>>>(); 


// now the place where u receive or fill arraylist then use this like 

DataController datacntrl; // global declare this 

 public ArrayList<CommanWrapperForListing> arraylist_wrapper_local = new ArrayList<CommanWrapperForListing>();//global same as datacotrller have 

datacntrl = DataController.getInstance(); // in oncreate()

//then fill your arraylist which is same as u declare in datacontroller getting my point means both are same type of arraylist 

//then just put this data type into the datacontroller class like this 

datacntrl.arraylist_wrapper_datacntrlr .add(new CommanWrapperForListing(new File(path).getName(), path, "Audio",false,color_string));

//or you can also do this 
controller.arraylist_wrapper_datacntrlr = arraylist_wrapper_local ;//once you fill this local arraylist then put it into datacontroller class varible 

//now just use this datacontroller arraylist it will never loss untill n unless u not delete it into stack 

 //use like 

//datacntrl.arraylist_wrapper_datacntrlr ; as we use static member of any class 

i try my best to describe this best of luck dear :)

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • i use it in my application sometime and it will work with map :) – Bhanu Sharma Dec 11 '13 at 13:14
  • Thanks for the response . I used DataController class in my code as suggested by you .But i'm still getting the same issue but less often .please help – Rachita Nanda Dec 12 '13 at 07:14
  • just chk one validation of arraylist!=null when u use that arraylist and if null then again reload data and fill them into list :) – Bhanu Sharma Dec 12 '13 at 08:11
  • Thanks ,but I don't want to do this as I have added data in arraylist based on some criteria from db.Also i will we unnecessarily reloading data into array list which is very bulky process .I want a solution to PERSIST arraylist so that they dont loose value and become null.:) – Rachita Nanda Dec 12 '13 at 08:27