0

私はAndroidマップ指向のプロジェクトを行っています。2つの場所の間の方向を表示するコードをいくつか作成しましたが、2.0、2.2などの下位のAndroidバージョンでのみ機能します。Android4.0以降では機能しません。このコードをAndroid4.0で実行すると、が表示されますIndexOutOfBoundsException

私のクラス

import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.widget.Button;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class LocationActivity extends MapActivity {
        MapView mv;

        MapController mc;

        Button Save, Cancel;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.map_places);
                MapView mv = (MapView)findViewById(R.id.mapView);
                mv.setBuiltInZoomControls(true);
                MapController mc = mv.getController();
                ArrayList<GeoPoint> all_geo_points = getDirections(10.154929, 76.390316, 10.015861, 76.341867);
                GeoPoint moveTo = all_geo_points.get(0);
                mc.animateTo(moveTo);
                mc.setZoom(12);
                mv.getOverlays().add(new MyOverlay(all_geo_points));
        }

        public class MyOverlay extends Overlay {
                private ArrayList<GeoPoint> all_geo_points;

                public MyOverlay(ArrayList<GeoPoint> allGeoPoints) {
                        super();
                        this.all_geo_points = allGeoPoints;
                }

                @Override
                public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
                        super.draw(canvas, mv, shadow);
                        drawPath(mv, canvas);
                        return true;
                }

                public void drawPath(MapView mv, Canvas canvas) {
                        int xPrev = -1, yPrev = -1, xNow = -1, yNow = -1;
                        Paint paint = new Paint();
                        paint.setColor(Color.BLUE);
                        paint.setStyle(Paint.Style.FILL_AND_STROKE);
                        paint.setStrokeWidth(4);
                        paint.setAlpha(100);
                        if (all_geo_points != null) for (int i = 0; i < all_geo_points.size() - 4; i++) {
                                GeoPoint gp = all_geo_points.get(i);
                                Point point = new Point();
                                mv.getProjection().toPixels(gp, point);
                                xNow = point.x;
                                yNow = point.y;
                                if (xPrev != -1) {
                                        canvas.drawLine(xPrev, yPrev, xNow, yNow, paint);
                                }
                                xPrev = xNow;
                                yPrev = yNow;
                        }
                }
        }

        public static ArrayList<GeoPoint> getDirections(double lat1, double lon1, double lat2, double lon2) {
                String url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2
                                + "&sensor=false&units=metric";
                String tag[] = {"lat", "lng"};
                ArrayList<GeoPoint> list_of_geopoints = new ArrayList<GeoPoint>();
                HttpResponse response = null;
                try {
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpContext localContext = new BasicHttpContext();
                        HttpPost httpPost = new HttpPost(url);
                        response = httpClient.execute(httpPost, localContext);
                        InputStream in = response.getEntity().getContent();
                        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                        Document doc = builder.parse(in);
                        if (doc != null) {
                                NodeList nl1, nl2;
                                nl1 = doc.getElementsByTagName(tag[0]);
                                nl2 = doc.getElementsByTagName(tag[1]);
                                if (nl1.getLength() > 0) {
                                        list_of_geopoints = new ArrayList<GeoPoint>();
                                        for (int i = 0; i < nl1.getLength(); i++) {
                                                Node node1 = nl1.item(i);
                                                Node node2 = nl2.item(i);
                                                double lat = Double.parseDouble(node1.getTextContent());
                                                double lng = Double.parseDouble(node2.getTextContent());
                                                list_of_geopoints.add(new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)));
                                        }
                                } else {
                                        // No points found
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return list_of_geopoints;
        }

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

私のlogcat

03-01 19:05:19.180: E/AndroidRuntime(2839): FATAL EXCEPTION: main
03-01 19:05:19.180: E/AndroidRuntime(2839): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kodspider.parkon/com.kodspider.parkon.LocationActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1900)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.widget.TabHost.setCurrentTab(TabHost.java:358)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.view.View.performClick(View.java:4084)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.view.View$PerformClick.run(View.java:16966)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.os.Handler.handleCallback(Handler.java:615)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.os.Looper.loop(Looper.java:137)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at java.lang.reflect.Method.invokeNative(Native Method)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at java.lang.reflect.Method.invoke(Method.java:511)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at dalvik.system.NativeStart.main(Native Method)
03-01 19:05:19.180: E/AndroidRuntime(2839): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-01 19:05:19.180: E/AndroidRuntime(2839):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at java.util.ArrayList.get(ArrayList.java:304)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at com.kodspider.parkon.LocationActivity.onCreate(LocationActivity.java:43)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.app.Activity.performCreate(Activity.java:5008)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-01 19:05:19.180: E/AndroidRuntime(2839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-01 19:05:19.180: E/AndroidRuntime(2839):     ... 18 more
4

4 に答える 4

3

したがって、基本的に、getDirections()は空のArrayListを返し、最初の項目にアクセスしようとしています。

メソッドが空のセットを返す理由を理解し、それを修正してください。

于 2013-03-01T13:49:26.783 に答える
0

com.kodspider.parkon.LocationActivity.onCreate(LocationActivity.java:43)で例外が発生しました

次のコード行だと思います。

ArrayList<GeoPoint> all_geo_points = getDirections(10.154929, 76.390316, 10.015861, 76.341867);
GeoPoint moveTo = all_geo_points.get(0);

したがって、メソッドgetDirectionsは空の配列を返します。試す:

 if(all_geo_points.size()>0){
    GeoPoint moveTo = all_geo_points.get(0);
    mc.animateTo(moveTo);
    mc.setZoom(12);
    mv.getOverlays().add(new MyOverlay(all_geo_points));
}
于 2013-03-01T13:51:03.573 に答える
0

上位バージョンのAndroidプラットフォームに関する質問の答えを見つけました。バックグラウンドスレッドにAsyncTaskクラスを使用する必要があります

于 2013-03-01T16:05:04.090 に答える
0

以下のコードを使用してください...

public static JSONObject getLocationInfo(String origin, String dest) {

StringBuilder stringBuilder = new StringBuilder();

try {
        origin = origin.replaceAll(" ", "%20");
        dest = dest.replaceAll(" ", "%20");

        HttpPost httppost = new HttpPost(
                "http://maps.googleapis.com/maps/api/directions/json?origin="
                        + origin + "&destination=" + dest + "&sensor=false");
        // http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&sensor=false

        Log.i("httppost",
                "http://maps.googleapis.com/maps/api/directions/json?origin="
                        + origin + "&destination=" + dest + "&sensor=false");

        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        stringBuilder = new StringBuilder();

        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {

        Log.i("Ex1", e.getMessage());

    } catch (IOException e) {
        Log.i("Ex2", e.getMessage());
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());

        Log.i("JSON Length", "" + jsonObject.length());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}
于 2013-04-30T11:12:03.473 に答える