无法启动活动ComponentInfo,MapActivity Android

时间:2012-05-03 22:01:09

标签: android mapactivity

我是Android编程的新手,我正在尝试在互联网上关注如何在Android上制作谷歌地图驾驶方向的一些教程。但不知怎的,代码没有用,虽然我的eclipse没有在代码上显示任何错误。如果这段代码有问题,任何人都可以提供帮助吗?

这是我的MapDirectionActivity

    package my.map.direction;


    import java.net.HttpURLConnection;
    //import java.net.URI;
    import java.net.URL;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    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 android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;

    public class MapDirectionActivity extends MapActivity {


        MapView myMapView = null;
        MapController myMc = null;
        GeoPoint geoPoint = null;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            myMapView = (MapView) findViewById(R.id.mapView);
            geoPoint = null;
            myMapView.setSatellite(false);

            String htw = "-6.3039,106.8267";
            String storkower = "-6.29436,106.8859";
            String pairs[] = getDirectionData(htw, storkower);
            String[] lngLat = pairs[0].split(",");

            //starting point
            GeoPoint startGP = new GeoPoint((int) (Double.parseDouble(lngLat[1])*1E6), (int)(Double.parseDouble(lngLat[0])*1E6));

            myMc = myMapView.getController();
            geoPoint = startGP;
            myMc.setCenter(geoPoint);
            myMc.setZoom(14);
            myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));

            GeoPoint gp1;
            GeoPoint gp2 = startGP;

            for(int i = 1; i<pairs.length;i++){
                lngLat = pairs[i].split(",");
                gp1 = gp2;

                gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6), (int)(Double.parseDouble(lngLat[0])*1E6));
                myMapView.getOverlays().add(new DirectionPathOverlay(gp1,gp2));
                Log.d("xxx","pair"+pairs[i]);
            }

            myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));
            myMapView.getController().animateTo(startGP);
            myMapView.setBuiltInZoomControls(true);
            myMapView.displayZoomControls(true);

        }

        @Override
        protected boolean isRouteDisplayed(){
            return false;
        }


        private String[] getDirectionData(String srcPlace, String destPlace){
            String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="
                    + srcPlace + "&daddr=" + destPlace
                    + "&ie=UTF8&0&om=0&output=kml";

        Log.d("URL",urlString); 

        Document doc = null;
        HttpURLConnection urlConnection = null;
        URL url = null;
        String pathContent = "";

        try{
            url = new URL(urlString.toString());
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.connect();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(urlConnection.getInputStream());     
        }catch(Exception e){

        }

        NodeList nl = doc.getElementsByTagName("LineString");
        for(int s=0; s< nl.getLength();s++){
            Node rootNode = nl.item(s);
            NodeList configItems = rootNode.getChildNodes();
            for(int x=0;x<configItems.getLength();x++){
                Node lineStringNode = configItems.item(x);
                NodeList path = lineStringNode.getChildNodes();
                pathContent = path.item(0).getNodeValue();
            }
        }

        String[] tempContent = pathContent.split(" ");
        return tempContent;
        }

    }

我的DirectionPathOverlay

public class DirectionPathOverlay extends Overlay{

    private GeoPoint gp1;
    private GeoPoint gp2;

    public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2){
        this.gp1 = gp1;
        this.gp2 = gp2;

    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){

        Projection projection = mapView.getProjection();
        if(shadow == false){
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            Point point = new Point();
            projection.toPixels(gp1, point);
            paint.setColor(Color.BLUE);
            Point point2 = new Point();
            projection.toPixels(gp2, point2);
            paint.setStrokeWidth(3);
            canvas.drawLine((float) point.x, (float) point.y, (float) point2.x, (float) point2.y, paint);
        }
    return super.draw(canvas, mapView, shadow, when);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow){

        super.draw(canvas, mapView, shadow);
    }
}

我在logcat上遇到以下错误

05-02 08:51:09.972: I/dalvikvm(629): threadid=3: reacting to signal 3
05-02 08:51:09.982: I/dalvikvm(629): Wrote stack traces to '/data/anr/traces.txt'
05-02 08:51:10.062: D/URL(629): http://maps.google.com/maps?f=d&hl=en&saddr=52.458402,13.526616&daddr=52.523559,13.475826&ie=UTF8&0&om=0&output=kml
05-02 08:51:10.082: D/AndroidRuntime(629): Shutting down VM
05-02 08:51:10.102: W/dalvikvm(629): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-02 08:51:10.122: E/AndroidRuntime(629): FATAL EXCEPTION: main
05-02 08:51:10.122: E/AndroidRuntime(629): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.google.maps/my.google.maps.MapsTestActivity}: java.lang.NullPointerException
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.os.Looper.loop(Looper.java:137)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-02 08:51:10.122: E/AndroidRuntime(629):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 08:51:10.122: E/AndroidRuntime(629):  at java.lang.reflect.Method.invoke(Method.java:511)
05-02 08:51:10.122: E/AndroidRuntime(629):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-02 08:51:10.122: E/AndroidRuntime(629):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-02 08:51:10.122: E/AndroidRuntime(629):  at dalvik.system.NativeStart.main(Native Method)
05-02 08:51:10.122: E/AndroidRuntime(629): Caused by: java.lang.NullPointerException
05-02 08:51:10.122: E/AndroidRuntime(629):  at my.google.maps.MapsTestActivity.getDirectionData(MapsTestActivity.java:100)
05-02 08:51:10.122: E/AndroidRuntime(629):  at my.google.maps.MapsTestActivity.onCreate(MapsTestActivity.java:38)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.app.Activity.performCreate(Activity.java:4465)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-02 08:51:10.122: E/AndroidRuntime(629):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-02 08:51:10.122: E/AndroidRuntime(629):  ... 11 more
05-02 08:51:10.152: D/dalvikvm(629): GC_CONCURRENT freed 206K, 5% free 5848K/6151K, paused 5ms+4ms
05-02 08:51:10.172: W/CursorWrapperInner(629): Cursor finalized without prior close()
05-02 08:51:10.172: W/CursorWrapperInner(629): Cursor finalized without prior close()
05-02 08:51:10.472: I/dalvikvm(629): threadid=3: reacting to signal 3
05-02 08:51:10.482: I/dalvikvm(629): Wrote stack traces to '/data/anr/traces.txt'
05-02 08:51:10.702: I/dalvikvm(629): threadid=3: reacting to signal 3
05-02 08:51:10.702: I/dalvikvm(629): Wrote stack traces to '/data/anr/traces.txt'
05-02 08:51:12.482: I/Process(629): Sending signal. PID: 629 SIG: 9
05-02 09:05:07.352: I/dalvikvm(647): threadid=3: reacting to signal 3
05-02 09:05:07.372: I/dalvikvm(647): Wrote stack traces to '/data/anr/traces.txt'
05-02 09:05:07.802: I/dalvikvm(647): threadid=3: reacting to signal 3
05-02 09:05:07.832: I/dalvikvm(647): Wrote stack traces to '/data/anr/traces.txt'
05-02 09:05:07.942: D/URL(647): http://maps.google.com/maps?f=d&hl=en&saddr=52.458402,13.526616&daddr=52.523559,13.475826&ie=UTF8&0&om=0&output=kml
05-02 09:05:07.972: D/AndroidRuntime(647): Shutting down VM
05-02 09:05:07.972: W/dalvikvm(647): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-02 09:05:07.982: E/AndroidRuntime(647): FATAL EXCEPTION: main
05-02 09:05:07.982: E/AndroidRuntime(647): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.google.maps/my.google.maps.MapsTestActivity}: java.lang.NullPointerException
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.os.Looper.loop(Looper.java:137)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-02 09:05:07.982: E/AndroidRuntime(647):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 09:05:07.982: E/AndroidRuntime(647):  at java.lang.reflect.Method.invoke(Method.java:511)
05-02 09:05:07.982: E/AndroidRuntime(647):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-02 09:05:07.982: E/AndroidRuntime(647):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-02 09:05:07.982: E/AndroidRuntime(647):  at dalvik.system.NativeStart.main(Native Method)
05-02 09:05:07.982: E/AndroidRuntime(647): Caused by: java.lang.NullPointerException
05-02 09:05:07.982: E/AndroidRuntime(647):  at my.google.maps.MapsTestActivity.getDirectionData(MapsTestActivity.java:100)
05-02 09:05:07.982: E/AndroidRuntime(647):  at my.google.maps.MapsTestActivity.onCreate(MapsTestActivity.java:38)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.app.Activity.performCreate(Activity.java:4465)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-02 09:05:07.982: E/AndroidRuntime(647):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-02 09:05:07.982: E/AndroidRuntime(647):  ... 11 more
05-02 09:05:08.032: D/dalvikvm(647): GC_CONCURRENT freed 206K, 5% free 5848K/6151K, paused 4ms+7ms
05-02 09:05:08.032: W/CursorWrapperInner(647): Cursor finalized without prior close()
05-02 09:05:08.032: W/CursorWrapperInner(647): Cursor finalized without prior close()
05-02 09:05:08.302: I/dalvikvm(647): threadid=3: reacting to signal 3
05-02 09:05:08.342: I/dalvikvm(647): Wrote stack traces to '/data/anr/traces.txt'
05-02 09:05:08.632: I/dalvikvm(647): threadid=3: reacting to signal 3
05-02 09:05:08.654: I/dalvikvm(647): Wrote stack traces to '/data/anr/traces.txt'

我已经检查了我的main.xml和manifest.xml以及运行谷歌地图的任何必要设置,我已经在我的另一个mapactivity programm上测试了xml的类似设置并且它正常工作,所以我不认为我的apikey没有工作

1 个答案:

答案 0 :(得分:0)

如果你查看日志,你会看到:

引起:java.lang.NullPointerException 05-02 09:05:07.982:E / AndroidRuntime(647):at my.google.maps.MapsTestActivity.getDirectionData(MapsTestActivity.java:100)

所以问题是函数getDirectionData返回NULL。