3

私はこの素晴らしい例を参照しています:こちらをご覧ください

次のエラーが発生し、解決策が見つかりません。助けてください!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>

エラーログ:

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)
4

3 に答える 3

1

UIスレッドでネットワークメソッドを実行できないため、メソッドdirections()を別のスレッドに配置する必要があります。AsyncTaskまた、 を使用mapView.invalidate()して更新 (オーバーレイを再描画) し、描画MapView後にルートを表示します。

于 2012-11-20T07:51:34.020 に答える
0

Route r = parser.parse();が空のルートを返していると思います(つまり、内部にポイントがありません)。

確認するには、上記の行の後に次を追加してみてください。

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

そしておそらくあなたは size を得るでしょう0

この場合、パーサーがパス ポイントを処理できない理由を確認する必要があります。

よろしく。

于 2012-11-19T16:02:16.577 に答える
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();


}

}
于 2012-11-20T11:01:15.397 に答える