我正在引用这个很好的例子:See here
我收到以下错误,但找不到解决方法。请帮忙! 将触发NoSuchElementException。在MainActivity中,我按照描述进行操作。在我的Android-Manifest中,我还包括
<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)
答案 0 :(得分:1)
您要做的是使用directions()
将方法AsyncTask
放在另一个线程中,因为您无法在UI线程上运行网络方法。另外,使用mapView.invalidate()
来刷新(重绘覆盖图)MapView
并在绘制后查看路线。
答案 1 :(得分:0)
我相信第Route r = parser.parse();
行返回一条空路线(即内部没有点)。
要确认,请尝试在上述行之后添加以下内容:
Lod.d("MyApp", "Route size: " + r.getPoints().size());
可能你会得到尺寸0
。
如果是这种情况,您需要解释为什么解析器无法处理路径点。
问候。
答案 2 :(得分: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();
}
}