0
package com.hands; 

import java.net.HttpURLConnection; 
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 android.os.Bundle; 
import android.util.Log; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 

public class DrawlineActivity 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); 
     double fromLat = 12.303534; 
     double fromLong = 76.64611; 
     double toLat = 12.9715987; 
     double toLong = 77.5945627; 

     String sourceLat = Double.toString(fromLat); 
     String sourceLong = Double.toString(fromLong); 
     String destinationLat = Double.toString(toLat); 
     String destinationLong = Double.toString(toLong); 

    String pairs[] = getDirectionData(sourceLat,sourceLong, destinationLat, destinationLong); 
//The above line pairs[] retrieving null value. Showing as NULL POINTER EXCEPTION 
     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(10); 
     myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP)); 

     // NAVIGATE THE PATH 

     GeoPoint gp1; 
     GeoPoint gp2 = startGP; 

     for (int i = 1; i < pairs.length; i++) { 
     lngLat = pairs[i].split(","); 
     gp1 = gp2; 
     // watch out! For GeoPoint, first:latitude, second:longitude 

     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]); 
     } 

     // END POINT 
     myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2)); 

     myMapView.getController().animateTo(startGP); 
     myMapView.setBuiltInZoomControls(true); 
     myMapView.displayZoomControls(true); 

    } 

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

佈局獲取問題在Android SDK中兩個位置之間的地圖繪製線

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

    <com.google.android.maps.MapView 
      android:id="@+id/mapview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:apiKey="034BXAeVNYe1Ob063cZUpablCB_0y7xjEGD3RhQ" 
      /> 

logcat的詳細

08-10 09:03:20.216: W/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
08-10 09:03:20.246: E/AndroidRuntime(305): FATAL EXCEPTION: main 
08-10 09:03:20.246: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hands/com.hands.DrawlineActivity}: java.lang.NullPointerException 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.os.Looper.loop(Looper.java:123) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at dalvik.system.NativeStart.main(Native Method) 
08-10 09:03:20.246: E/AndroidRuntime(305): Caused by: java.lang.NullPointerException 
08-10 09:03:20.246: E/AndroidRuntime(305):  at com.hands.DrawlineActivity.getDirectionData(DrawlineActivity.java:111) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at com.hands.DrawlineActivity.onCreate(DrawlineActivity.java:44) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-10 09:03:20.246: E/AndroidRuntime(305):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
08-10 09:03:20.246: E/AndroidRuntime(305):  ... 11 more 

更新了getDirecti on方法代碼

package com.hands;

import java.net.HttpURLConnection; 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 android.os.Bundle; import android.util.Log;

import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView;

public class DrawlineActivity extends MapActivity {0} {0} {0} 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); 
    double fromLat = 12.303534; 
    double fromLong = 76.64611; 
    double toLat = 12.9715987; 
    double toLong = 77.5945627; 

    String sourceLat = Double.toString(fromLat); 
    String sourceLong = Double.toString(fromLong); 
    String destinationLat = Double.toString(toLat); 
    String destinationLong = Double.toString(toLong); 

    String pairs[] = getDirectionData(sourceLat,sourceLong, destinationLat, destinationLong); 
    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(10); 
    myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP)); 

    // NAVIGATE THE PATH 

    GeoPoint gp1; 
    GeoPoint gp2 = startGP; 

    for (int i = 1; i < pairs.length; i++) { 
    lngLat = pairs[i].split(","); 
    gp1 = gp2; 
    // watch out! For GeoPoint, first:latitude, second:longitude 

    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]); 
    } 

    // END POINT 
    myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2)); 

    myMapView.getController().animateTo(startGP); 
    myMapView.setBuiltInZoomControls(true); 
    myMapView.displayZoomControls(true); 

} 

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

private String[] getDirectionData(String sourceLat, String sourceLong, String destinationLat, String destinationLong) { 


    String urlString = "http://maps.google.com/maps?f=d&hl=en&" +"saddr="+sourceLat+","+sourceLong+"&daddr="+destinationLat+","+destinationLong + "&ie=UTF8&0&om=0&output=kml"; 
    Log.d("URL", urlString); 
    Document doc = null; 
    HttpURLConnection urlConnection = null; 
    URL url = null; 
    String pathConent = ""; 

    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(); 
    pathConent = path.item(0).getNodeValue(); 
    } 
    } 
    String[] tempContent = pathConent.split(" "); 
    return tempContent; 
} 

} 

回答

0

正如logcat解釋的那樣,當您調用方法getDirectionData()時,您會得到NullPointerException。以上代碼中未顯示此方法。你可以添加這種方法,以便我們可以看到問題是什麼?

編輯:感謝您添加您的getDirectionData()方法。它仍然不清楚問題出在哪裏,但有一個問題是你有一個空的catch塊 - 這將吞噬異常,然後你繼續處理代碼。例如,如果線路

doc = db.parse(urlConnection.getInputStream()); 

拋出一個異常或返回null,然後嘗試得到一個空對象節點列表 - 這將給你的空指針異常。我建議在此方法開始時加上一個斷點並逐步完成,隨時檢查變量。這將確定問題的根源。

+0

我用getDirectionData()更新了我的問題,因爲它給出了空指針異常....所以現在可以請你檢查這段代碼並糾正我的代碼..你可以給我一些路由顯示的例子..... ... – 2012-08-13 03:52:01

+0

更新了我的答案。 – 2012-08-13 08:57:05

相關問題