获取问题在Android SDK中的两个位置之间画线的地图 [英] Getting Issue drawing Line in Map between Two locations in android sdk
问题描述
包com.hands;
进口java.net.HttpURLConnection中;
进口的java.net.URL;
进口javax.xml.parsers.DocumentBuilder中;
进口javax.xml.parsers.DocumentBuilderFactory中;
进口org.w3c.dom.Document中;
进口org.w3c.dom.Node中;
进口org.w3c.dom.NodeList;
进口android.os.Bundle;
进口android.util.Log;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
公共类DrawlineActivity扩展MapActivity {
图形页面myMapView = NULL;
MapController myMC = NULL;
GeoPoint对象的GeoPoint = NULL;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
myMapView =(图形页面)findViewById(R.id.mapview);
的GeoPoint = NULL;
myMapView.setSatellite(假);
双fromLat = 12.303534;
双fromLong = 76.64611;
双toLat = 12.9715987;
双toLong = 77.5945627;
字符串sourceLat = Double.toString(fromLat);
字符串sourceLong = Double.toString(fromLong);
字符串destinationLat = Double.toString(toLat);
字符串destinationLong = Double.toString(toLong);
字符串对[] = getDirectionData(sourceLat,sourceLong,destinationLat,destinationLong);
//以上线对[]检索空值。显示为空指针异常
串[] lngLat =双[0] .split(,);
// 初始点
的GeoPoint startGP =新的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()增加(新DirectionPathOverlay(startGP,startGP));
//浏览路径
的GeoPoint GP1;
的GeoPoint GP2 = startGP;
的for(int i = 1; I< pairs.length;我++){
lngLat =对[I] .split(,);
GP1 = GP2;
// 小心!对于GeoPoint对象,第一:纬度,第二:经度
GP2 =新的GeoPoint((int)的(Double.parseDouble(lngLat [1])* 1E6),(int)的(Double.parseDouble(lngLat [0])* 1E6));
。myMapView.getOverlays()增加(新DirectionPathOverlay(GP1,GP2));
Log.d(XXX,对:+对[I]);
}
//终点
。myMapView.getOverlays()增加(新DirectionPathOverlay(GP2,GP2));
。myMapView.getController()animateTo(startGP);
myMapView.setBuiltInZoomControls(真正的);
myMapView.displayZoomControls(真正的);
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
布局
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/你好/>
< com.google.android.maps.MapView
机器人:ID =@ + ID /图形页面
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:apiKey =034BXAeVNYe1Ob063cZUpablCB_0y7xjEGD3RhQ
/>
logcat的详细信息
9月八号至10日:03:20.216:W / dalvikvm(305):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d800)
九月八日至10日:03:20.246:E / AndroidRuntime(305):致命异常:主要
九月八日至10日:03:20.246:E / AndroidRuntime(305):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.hands / com.hands.DrawlineActivity}:显示java.lang.NullPointerException
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.os.Handler.dispatchMessage(Handler.java:99)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.os.Looper.loop(Looper.java:123)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.app.ActivityThread.main(ActivityThread.java:4627)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在java.lang.reflect.Method.invokeNative(本机方法)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在java.lang.reflect.Method.invoke(Method.java:521)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在dalvik.system.NativeStart.main(本机方法)
九月八日至10日:03:20.246:E / AndroidRuntime(305):由:显示java.lang.NullPointerException
九月八日至10日:03:20.246:E / AndroidRuntime(305):在com.hands.DrawlineActivity.getDirectionData(DrawlineActivity.java:111)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在com.hands.DrawlineActivity.onCreate(DrawlineActivity.java:44)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
九月八日至10日:03:20.246:E / AndroidRuntime(305):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
九月八日至10日:03:20.246:E / AndroidRuntime(305):11 ...更多
更新getDirection方法code
包com.hands;
进口java.net.HttpURLConnection中; 进口的java.net.URL;
进口javax.xml.parsers.DocumentBuilder中; 进口javax.xml.parsers.DocumentBuilderFactory中;
进口org.w3c.dom.Document中; 进口org.w3c.dom.Node中; 进口org.w3c.dom.NodeList;
进口android.os.Bundle; 进口android.util.Log;
进口com.google.android.maps.GeoPoint; 进口com.google.android.maps.MapActivity; 进口com.google.android.maps.MapController; 进口com.google.android.maps.MapView;
公共类DrawlineActivity扩展MapActivity { 图形页面myMapView = NULL; MapController myMC = NULL; GeoPoint对象的GeoPoint = NULL; @覆盖 公共无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
myMapView =(图形页面)findViewById(R.id.mapview);
的GeoPoint = NULL;
myMapView.setSatellite(假);
双fromLat = 12.303534;
双fromLong = 76.64611;
双toLat = 12.9715987;
双toLong = 77.5945627;
字符串sourceLat = Double.toString(fromLat);
字符串sourceLong = Double.toString(fromLong);
字符串destinationLat = Double.toString(toLat);
字符串destinationLong = Double.toString(toLong);
字符串对[] = getDirectionData(sourceLat,sourceLong,destinationLat,destinationLong);
串[] lngLat =双[0] .split(,);
// 初始点
的GeoPoint startGP =新的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()增加(新DirectionPathOverlay(startGP,startGP));
//浏览路径
的GeoPoint GP1;
的GeoPoint GP2 = startGP;
的for(int i = 1; I< pairs.length;我++){
lngLat =对[I] .split(,);
GP1 = GP2;
// 小心!对于GeoPoint对象,第一:纬度,第二:经度
GP2 =新的GeoPoint((int)的(Double.parseDouble(lngLat [1])* 1E6),(int)的(Double.parseDouble(lngLat [0])* 1E6));
。myMapView.getOverlays()增加(新DirectionPathOverlay(GP1,GP2));
Log.d(XXX,对:+对[I]);
}
//终点
。myMapView.getOverlays()增加(新DirectionPathOverlay(GP2,GP2));
。myMapView.getController()animateTo(startGP);
myMapView.setBuiltInZoomControls(真正的);
myMapView.displayZoomControls(真正的);
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
私有String [] getDirectionData(字符串sourceLat,字符串sourceLong,字符串destinationLat,字符串destinationLong){
字符串urlString =http://maps.google.com/maps?f=d&hl=en& +SADDR =+ sourceLat +,+ sourceLong +与& DADDR =+ destinationLat +,+ destinationLong +与&即= UTF8&安培0安培;嗡= 0&安培;输出= KML;
Log.d(URL,urlString);
文档DOC = NULL;
HttpURLConnection类的URLConnection = NULL;
网址URL = NULL;
字符串pathConent =;
尝试 {
URL =新的URL(urlString.toString());
的URLConnection =(HttpURLConnection类)url.openConnection();
urlConnection.setRequestMethod(GET);
urlConnection.setDoOutput(真正的);
urlConnection.setDoInput(真正的);
urlConnection.connect();
DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
DocumentBuilder的DB = dbf.newDocumentBuilder();
文档= db.parse(urlConnection.getInputStream());
}赶上(例外五){
}
NodeList的NL = doc.getElementsByTagName(的LineString);
对于(INT S = 0; S< nl.getLength(); S ++){
节点rootNode中= nl.item(S);
节点列表configItems = rootNode.getChildNodes();
为(中间体X = 0 X - 其中; configItems.getLength(); X ++){
节点lineStringNode = configItems.item(X);
节点列表路径= lineStringNode.getChildNodes();
pathConent = path.item(0).getNodeValue();
}
}
的String [] tempContent = pathConent.split();
返回tempContent;
}
}
作为logcat的解释,你得到一个NullPointerException异常,当你调用方法getDirectionData()。此方法未在code如上所示。你可以把这个方法,所以我们可以看到的问题是什么?
编辑:感谢您的加入getDirectionData()方法。它仍不清楚是哪里的问题,但一个问题是,你有一个空的catch块 - 这将吞下的异常,然后你进行处理code。例如,如果线路
DOC = db.parse(urlConnection.getInputStream());
抛出一个异常,或返回null,然后尝试得到一个空对象的节点列表 - 这将给你的空指针异常。我建议把一个断点,这种方法的开始,并通过它加强,检查变量,当您去。这将确定问题的来源。
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;
}
Layout
<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 Details
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
Updated With getDirection Method code
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 );
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;
}
}
As the logcat explains, you are getting a NullPointerException when you call the method getDirectionData(). This method is not shown in the code above. Can you add this method so we can see what the problem is?
Edit: Thanks for adding your getDirectionData() method. Its still not clear where the problem is but one issue is the fact that you have an empty catch block - this will swallow exceptions and then you carry on processing code. For example, if the line
doc = db.parse(urlConnection.getInputStream());
throws an exception or returns null, then you try to get the NodeList from a null object - this would give you your null pointer exception. I recommend putting a breakpoint at the start of this method and stepping through it, examining the variables as you go. This will identify the source of the problem.
这篇关于获取问题在Android SDK中的两个位置之间画线的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!