2012-11-19 132 views
3

我指的這個偉大的例子:See here繪製在谷歌地圖MapView的一個路徑使用JSON

我收到以下錯誤,我無法找到一個解決方案。請幫忙! 觸發NoSuchElementException。在MainActivity中,我按照所描述的方式進行操作。在我的Android艙單我還包括

<uses-library android:name="com.google.android.maps" /> 

開始的MapView不久出現的應用程序(灰色圖案),然後突然出現關閉對話框力之後。

MainActivity

public class MainActivity extends MapActivity { 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    MapView mapView = (MapView) findViewById(R.id.mapview); //or you can declare it directly with the API key 
    Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6))); 
    RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE); 
    mapView.getOverlays().add(routeOverlay); 

} 

private Route directions(final GeoPoint start, final GeoPoint dest) { 
    Parser parser; 
    String jsonURL = "http://maps.google.com/maps/api/directions/json?"; 
    final StringBuffer sBuf = new StringBuffer(jsonURL); 
    sBuf.append("origin="); 
    sBuf.append(start.getLatitudeE6()/1E6); 
    sBuf.append(','); 
    sBuf.append(start.getLongitudeE6()/1E6); 
    sBuf.append("&destination="); 
    sBuf.append(dest.getLatitudeE6()/1E6); 
    sBuf.append(','); 
    sBuf.append(dest.getLongitudeE6()/1E6); 
    sBuf.append("&sensor=true&mode=driving"); 
    parser = new GoogleParser(sBuf.toString()); 
    Route r = parser.parse(); 
    return r; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 
} 

我activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<com.google.android.maps.MapView 
    android:id="@+id/mapview" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:clickable="true" 
    android:apiKey="..........................................." /> 

</RelativeLayout> 

Errolog

11-19 11:22:17.782: I/MapActivity(10703): Handling network change notification:CONNECTED 
11-19 11:22:17.782: E/MapActivity(10703): Couldn't get connection factory client 
11-19 11:22:17.822: D/AndroidRuntime(10703): Shutting down VM 
11-19 11:22:17.822: W/dalvikvm(10703): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) 
11-19 11:22:17.872: E/AndroidRuntime(10703): FATAL EXCEPTION: main 
11-19 11:22:17.872: E/AndroidRuntime(10703): java.util.NoSuchElementException 
11-19 11:22:17.872: E/AndroidRuntime(10703): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:579) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at com.example.testjsongooglemap.RouteOverlay.redrawPath(RouteOverlay.java:90) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at com.example.testjsongooglemap.RouteOverlay.draw(RouteOverlay.java:60) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at com.google.android.maps.Overlay.draw(Overlay.java:179) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at com.google.android.maps.MapView.onDraw(MapView.java:530) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.View.draw(View.java:7014) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewGroup.drawChild(ViewGroup.java:1732) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewGroup.drawChild(ViewGroup.java:1730) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.View.draw(View.java:7017) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.widget.FrameLayout.draw(FrameLayout.java:357) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewGroup.drawChild(ViewGroup.java:1732) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewGroup.drawChild(ViewGroup.java:1730) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.View.draw(View.java:7017) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.widget.FrameLayout.draw(FrameLayout.java:357) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2054) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewRoot.draw(ViewRoot.java:1632) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewRoot.performTraversals(ViewRoot.java:1335) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.view.ViewRoot.handleMessage(ViewRoot.java:1991) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.os.Looper.loop(Looper.java:150) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at android.app.ActivityThread.main(ActivityThread.java:4385) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at java.lang.reflect.Method.invokeNative(Native Method) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at java.lang.reflect.Method.invoke(Method.java:507) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
11-19 11:22:17.872: E/AndroidRuntime(10703): at dalvik.system.NativeStart.main(Native Method) 

回答

1

你想要做的就是把方法directions()放在另一個使用AsyncTask的線程中,因爲你不能在UI線程上運行網絡方法。此外,使用mapView.invalidate()爲了刷新(重新繪製覆蓋圖)MapView並在繪製它之後看到路線。

0

我相信線Route r = parser.parse();將返回一個空航線(即沒有內點)。

要確認,嘗試上面行後添加以下:

Lod.d("MyApp", "Route size: " + r.getPoints().size()); 

,也許你會得到大小0

如果是這種情況,您需要理解爲什麼解析器無法處理您的路徑點。

問候。

0

這裏是如何看起來像:

public class MainActivity extends MapActivity { 

MapView mapView = null; 
MapController mapController; 
double long1 = 50.123*1E6; 
double lati1 = 13.123*1E6; 
double long2 = 50.221*1E6; 
double lati2 = 13.221*1E6; 
GeoPoint start, dest; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mapView = (MapView) findViewById(R.id.mapview); 


    DirectionsTask getDirectionsTask = new DirectionsTask(); 
    getDirectionsTask.execute(new GeoPoint((int)(long1),(int)(lati1)), new GeoPoint((int)(long2),(int)(lati2))); 



} 

private class DirectionsTask extends AsyncTask<GeoPoint, Void, Route> { 

    protected Route doInBackground(GeoPoint...geoPoints) { 
     start = geoPoints[0]; 
     dest = geoPoints[1]; 

      Parser parser; 
      String jsonURL = "http://maps.google.com/maps/api/directions/json?"; 
      final StringBuffer sBuf = new StringBuffer(jsonURL); 
      sBuf.append("origin="); 
      sBuf.append(start.getLatitudeE6()/1E6); 
      sBuf.append(','); 
      sBuf.append(start.getLongitudeE6()/1E6); 
      sBuf.append("&destination="); 
      sBuf.append(dest.getLatitudeE6()/1E6); 
      sBuf.append(','); 
      sBuf.append(dest.getLongitudeE6()/1E6); 
      sBuf.append("&sensor=true&mode=driving"); 
      Log.v("I came in URL", sBuf.toString()); 
      parser = new GoogleParser(sBuf.toString()); 
      Route r = parser.parse(); 
      return r; 
    } 




protected void onPostExecute(Route route) { 
    RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE); 
    mapView.getOverlays().add(routeOverlay); 
    mapView.invalidate(); 
    mapController = mapView.getController(); 
    mapController.setZoom(14); // Zoom 1 is world view 
    mapView.getMapCenter(); 


} 

}