2012-05-03 264 views
0

我真的很新的Android編程,我想跟隨一些教程在互聯網上關於如何使谷歌地圖駕駛方向在Android上。但不知怎麼的代碼沒有工作,雖然我的eclipse沒有顯示任何代碼錯誤。如果在這段代碼中出現錯誤,任何人都可以幫忙無法啓動活動ComponentInfo,MapActivity Android

這是我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和明顯。 XML與任何必要的設置運行谷歌地圖,我已經測試類似的設置在我的另一個mapactivity PROGRAMM XML和它的工作,所以我不認爲我的apikey沒有工作

+0

從第100行開始的getDirectionData方法內部有一個空引用。在此方法中放置一個斷點並進行調試並逐步完成,找出造成問題的原因。 –

回答

0

如果你看一下日誌,你看:顯示java.lang.NullPointerException 05-:

所致02 09:05:07.982:E/AndroidRuntime(647):at my.google.maps.MapsTestActivity.getDirectionData(MapsTestActivity.java:100)

所以問題是函數getDirectionData返回NULL。

相關問題