バスが通過するすべてのバス停間の方向ルートを描画することにより、マップ上にバス ルートを描画しようとしています。
すべてのバス停の LatLng オブジェクトを含むリスト (markerPoints) を取得し、すべてのポイントのループを実行しようとします。各ポイントについて、ループの現在の位置にあるバス停との間の方向ルートを受け取り、次のもの。
問題は、このコードを実行すると Nullpointerexeption エラーが発生することがあることです。通常、最初に実行したときは機能しますが、次回はクラッシュします。ただし、ループを 1 回だけ実行するように変更して、1 方向のルートのみを取得すると、機能しています...
私の問題の解決策を見つけるのを手伝ってくれる人、または私が間違っていることを指摘してくれる人はいますか?
方向ルートを受信するための非同期メソッドを呼び出すループを使用した私のメソッド
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)