Polylineoptions Cannot Be Null
Solution 1:
I review your code and found some issue where causes of crash, first of all you had initialize null ArrayList<LatLng>points=null
and PolylineOptions lineOptions=null
so what if path is null then lineOptions
too null, thus i fix that line review below code apply it run it, if success Enjoy!
@Override
protectedvoid onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points = new ArrayList<LatLng>();;
PolylineOptions lineOptions = new PolylineOptions();;
lineOptions.width(2);
lineOptions.color(Color.RED);
MarkerOptions markerOptions = new MarkerOptions();
// Traversing through all the routesfor(int i=0;i<result.size();i++){
// Fetching i-th routeList<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th routefor(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
}
// Drawing polyline in the Google Map for the i-th routeif(points.size()!=0)mMap.addPolyline(lineOptions);//to avoid crash
}
Solution 2:
Why you are you initializing ArrayList,PolylineOption under for loop it will create new instance as many time for loop runs so try to fix these and add two mMap.moveCamera(CameraUpdateFactory.newLatLng(position)); mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
ArrayList<LatLng> points = new ArrayList<LatLng>();
PolylineOptions lineOptions = new PolylineOptions();;
MarkerOptions markerOptions = new MarkerOptions();
LatLng position = null;
// Traversing through all the routesfor(int i=0;i<result.size();i++){
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th routefor(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
lineOptions.addAll(points);
lineOptions.width(2);
lineOptions.color(Color.RED);
}
// Drawing polyline in the Google Map for the i-th route
mMap.addPolyline(lineOptions);
}
Solution 3:
you can avoid crash by simply adding this line in protected void onPostExecute(List>> result) method
enter code here
if(points)!=null {
mMap.addPolyline(lineOptions);
}
But because of error in your call to direction API it is returning null value to points. Check wether direction api is enabled correctly
Solution 4:
TRY THIS!!!
1. In any case you should add a check to see if polyLineOptions
is null before using it
if (polyLineOptions != null){
googleMap.addPolyline(polyLineOptions);
}
It will prevent from Crash
2. To make a route draw Add your API KEY at end of the getDirectionsUrl
like,
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters +"&key=" +"YOUR KEY";
Post a Comment for "Polylineoptions Cannot Be Null"