gpt4 book ai didi

java - Android Google Map V3 PolyLine无法绘制

转载 作者:行者123 更新时间:2023-11-29 04:55:15 27 4
gpt4 key购买 nike

尝试在 Google map 上匹配 PolyLine 时出现此错误

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

这是Logcat中提到的行

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

googleMap.addPolyline(polyLineOptions);

这是我的 Activity 代码:

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

如果在接收或解析数据时出现错误,这将确保您的应用不会崩溃。

关于java - Android Google Map V3 PolyLine无法绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037065/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com