尝试在Google Map上匹配PolyLine时遇到此错误
java.lang.NullPointerException:尝试从字段'float中读取
com.google.android.gms.maps.model.PolylineOptions.c'放在一个空对象上
参考
这是Logcat中提到的行
在
testappmapv2.PathGoogleMapActivity $ ParserTask.onPostExecute(PathGoogleMapActivity.java:208)
googleMap.addPolyline(polyLineOptions);
这是我的活动代码:
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);
}}}
我确保GeoPoints来自Parse Server。
任何帮助将不胜感激。
谢谢
最佳答案
看来polyLineOptions
从未实例化。
如果routes.size()
为0,则可能发生这种情况
您的问题似乎与传递给onPostExecute
方法的数据有关,因此请检查它是否有效以及是否已正确解析。
无论如何,您都应该在使用polyLineOptions
之前添加检查以查看它是否为空,例如
if (polyLineOptions != null)
{
googleMap.addPolyline(polyLineOptions);
}
如果接收或解析数据时出现错误,这将确保您的应用不会崩溃。