我试图在我的地图上绘制一条公共汽车路线,在公交车开出的所有公交车站之间绘制方向路线。
我有一个带有LatLng对象的列表(markerPoints)用于所有公交车站,并尝试为所有点运行一个循环,其中我为每个点接收当前位置的公交车站之间的方向路线循环和下一个。
问题是我在运行此代码时有时会出现Nullpointerexeption错误。它通常在我第一次运行时运行,然后在下次崩溃时运行。但是,如果我改变循环以便它只运行一次,因此只获得一个方向路线,那么它正在工作......
任何人都可以帮我找到问题的解决方案,或者指出我做错了什么?
我的方法,循环调用async mathods接收方向路径
public void DrawRoutePath(){
//Start on "Torget" because of issues with route path in city center
for (int i = 0; i < (markerPoints.size()-1); i++) {
LatLng origin = markerPoints.get(i);
LatLng dest = markerPoints.get(i+1);
// Getting URL to the Google Directions API
String url = getDirectionsUrl(origin, dest);
DownloadTask task = new DownloadTask();
// Start downloading json data from Google Directions API
task.execute(url);
}
}
这些是我获取方向异步的方法:
private String getDirectionsUrl(LatLng origin,LatLng dest){
// Origin of route
String str_origin = "origin="+origin.latitude+","+origin.longitude;
// Destination of route
String str_dest = "destination="+dest.latitude+","+dest.longitude;
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = str_origin+"&"+str_dest+"&"+sensor;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;
return url;
}
/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}
// Fetches data from url passed
private class DownloadTask extends AsyncTask<String, Void, String>{
// Downloading data in non-ui thread
@Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try{
// Fetching the data from web service
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
// Executes in UI thread, after the execution of
// doInBackground()
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{
// Parsing the data in non-ui thread
@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]);
DirectionsJSONParser parser = new DirectionsJSONParser();
// Starts parsing data
routes = parser.parse(jObject);
}catch(Exception e){
e.printStackTrace();
}
return routes;
}
// Executes in UI thread, after the parsing process
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points = null;
PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();
// Traversing through all the routes
for(int i=0;i<result.size();i++){
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
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);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(5);
lineOptions.color(Color.RED);
}
// Drawing polyline in the Google Map for the i-th route
mMap.addPolyline(lineOptions);
}
}
来自logcat的错误日志
04-22 20:03:18.911: E/AndroidRuntime(10471): FATAL EXCEPTION: main
04-22 20:03:18.911: E/AndroidRuntime(10471): java.lang.NullPointerException
04-22 20:03:18.911: E/AndroidRuntime(10471): at maps.z.ac.<init>(Unknown Source)
04-22 20:03:18.911: E/AndroidRuntime(10471): at maps.z.bi.a(Unknown Source)
04-22 20:03:18.911: E/AndroidRuntime(10471): at maps.z.bi.b(Unknown Source)
04-22 20:03:18.911: E/AndroidRuntime(10471): at maps.z.ag.addPolyline(Unknown Source)
04-22 20:03:18.911: E/AndroidRuntime(10471): at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:137)
04-22 20:03:18.911: E/AndroidRuntime(10471): at android.os.Binder.transact(Binder.java:326)
04-22 20:03:18.911: E/AndroidRuntime(10471): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addPolyline(Unknown Source)
04-22 20:03:18.911: E/AndroidRuntime(10471): at com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
04-22 20:03:18.911: E/AndroidRuntime(10471): at com.mso.master.MyMapFragment$ParserTask.onPostExecute(MyMapFragment.java:469)
04-22 20:03:18.911: E/AndroidRuntime(10471): at com.mso.master.MyMapFragment$ParserTask.onPostExecute(MyMapFragment.java:1)
04-22 20:03:18.911: E/AndroidRuntime(10471): at android.os.AsyncTask.finish(AsyncTask.java:631)
04-22 20:03:18.911: E/AndroidRuntime(10471): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-22 20:03:18.911: E/AndroidRuntime(10471): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-22 20:03:18.911: E/AndroidRuntime(10471): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 20:03:18.911: E/AndroidRuntime(10471): at android.os.Looper.loop(Looper.java:137)
04-22 20:03:18.911: E/AndroidRuntime(10471): at android.app.ActivityThread.main(ActivityThread.java:4931)
04-22 20:03:18.911: E/AndroidRuntime(10471): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 20:03:18.911: E/AndroidRuntime(10471): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 20:03:18.911: E/AndroidRuntime(10471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-22 20:03:18.911: E/AndroidRuntime(10471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-22 20:03:18.911: E/AndroidRuntime(10471): at dalvik.system.NativeStart.main(Native Method)