Android Google Map V3 PolyLine cannot be drawn

你。 提交于 2020-01-06 01:52:09

问题


Getting this error while trying to match PolyLine on Google Map

java.lang.NullPointerException: Attempt to read from field 'float com.google.android.gms.maps.model.PolylineOptions.c' on a null object reference

This is the line mentioned in Logcat

at testappmapv2.PathGoogleMapActivity$ParserTask.onPostExecute(PathGoogleMapActivity.java:208)

googleMap.addPolyline(polyLineOptions);

This is my Activity Code:

import android.app.AlarmManager; import android.app.PendingIntent;
import android.content.Context; import android.content.Intent;
import android.graphics.Color; import android.location.Location;               

import android.os.AsyncTask; import android.os.Bundle;  
import android.support.v4.app.FragmentActivity;  
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;  
import com.google.android.gms.maps.GoogleMap;  
import com.google.android.gms.maps.SupportMapFragment;  
import com.google.android.gms.maps.model.LatLng;  
import com.google.android.gms.maps.model.Marker;  
import com.google.android.gms.maps.model.MarkerOptions;  
import com.google.android.gms.maps.model.PolylineOptions;  
import com.parse.ParseException; 
import com.parse.ParseGeoPoint;  
import com.parse.ParseObject; 
import com.parse.ParseQuery;

import org.json.JSONObject;
import java.util.ArrayList; import java.util.HashMap; 
import java.util.List;

 public class PathGoogleMapActivity extends FragmentActivity {

         Location mCurrentLocation;
         String mLastUpdateTime;
         GoogleMap googleMap;
         LatLng visitedPoints, currentPoints;
         final String TAG = "PathGoogleMapActivity";
         MarkerOptions options;
         Marker marker;
         private final String DIRECTIONS_API_KEY = "*****************";
         private String parseClass = "LocationManager";
         private String User_id = "12345";
         private List<ParseObject> list;
         private List<LatLng> latLngList=new ArrayList<LatLng>();

         @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_path_google_map);
             SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                     .findFragmentById(R.id.map);
             googleMap = fm.getMap();
             googleMap.getUiSettings().setZoomControlsEnabled(true);        

         }



         @Override
         protected void onResume() {
             super.onResume();

             try {
                 ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery(parseClass);
                 parseQuery.whereEqualTo("User_Id", User_id);

                 list = parseQuery.find();

                 final ArrayList visitedPoint = (ArrayList) list.get(0).get("VisitedPoint");

                 for (Object parseGeoPoint : visitedPoint) {

                     ParseGeoPoint geoPoint = (ParseGeoPoint) parseGeoPoint;
                     LatLng latLng = new LatLng(geoPoint.getLatitude(), geoPoint.getLongitude());
                     latLngList.add(latLng);
                     Log.i(TAG, "parseGeoPoint" + latLng.toString());
                 }

                 googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLngList.get(latLngList.size()-1),
    15.0f));

                 Log.i(TAG, "Size of List: " + list.size());
                 Log.i(TAG, "ArrayList: " + visitedPoint.size());
                 Log.i(TAG, "ArrayList: " + visitedPoint.toString());


             } catch (ParseException e) {
                 e.printStackTrace();
             }

             for(int i=0;i<latLngList.size()-1;i++)
             {
                 String url = getMapsApiDirectionsUrl(latLngList.get(i), latLngList.get(i+1));
                 ReadTask downloadTask = new ReadTask();
                 downloadTask.execute(url);
                 addMarkers(latLngList.get(i+1));
             }    }



         private String getMapsApiDirectionsUrl(LatLng start, LatLng end) {

             String output = "json";
             String url = "https://maps.googleapis.com/maps/api/directions/json?"
                     + "origin=" + start.latitude + "," + start.longitude
                     + "&destination=" + end.latitude + "," + end.longitude
                     + "&sensor=false&units=metric&mode=walking&key=" + DIRECTIONS_API_KEY;
             return url;
         }

         private void addMarkers(LatLng latLng) {      

             marker = googleMap.addMarker(new MarkerOptions().position(latLng).title("You are here"));
         }

         private class ReadTask extends AsyncTask<String, Void, String> {
             @Override
             protected String doInBackground(String... url) {
                 String data = "";
                try {
                     HttpConnection http = new HttpConnection();
                     data = http.readUrl(url[0]);
                 } catch (Exception e) {
                     Log.d("Background Task", e.toString());
                 }
                 return data;
             }

             @Override
             protected void onPostExecute(String result) {
                 super.onPostExecute(result);
                 new ParserTask().execute(result);
             }
         }

         private class ParserTask extends
                 AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
             @Override
             protected List<List<HashMap<String, String>>> doInBackground(
                     String... jsonData) {
                 JSONObject jObject;
                 List<List<HashMap<String, String>>> routes = null;
                 try {
                     jObject = new JSONObject(jsonData[0]);
                     PathJSONParser parser = new PathJSONParser();
                     routes = parser.parse(jObject);
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
                 return routes;
             }

             @Override
             protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
                 ArrayList<LatLng> points = null;
                 PolylineOptions polyLineOptions = null;

                 // traversing through routes
                 for (int i = 0; i < routes.size(); i++) {
                     points = new ArrayList<LatLng>();
                     polyLineOptions = new PolylineOptions();
                     List<HashMap<String, String>> path = routes.get(i);

                     for (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);
                     }

                     polyLineOptions.addAll(points);
                    polyLineOptions.width(2);
                    polyLineOptions.color(Color.BLUE);
                 }

                 googleMap.addPolyline(polyLineOptions);
             }}}

I've ensured that GeoPoints are coming from Parse Server. Any Help will be appreciated. Thanks


回答1:


It looks like polyLineOptions never gets instantiated.

This can happen if routes.size() is 0

Your problem seems to be related to the data passed to the onPostExecute method, so check to see if it's valid and if it was parsed correctly

In any case you should add a check to see if polyLineOptions is null before using it, such as

if (polyLineOptions != null)
{
     googleMap.addPolyline(polyLineOptions);
}

This will make sure you app doesn't crash if there was an error in receiving or parsing the data.



来源:https://stackoverflow.com/questions/34037065/android-google-map-v3-polyline-cannot-be-drawn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!