Java 使用GMapsV2Direction类时出现NullPointerException
在过去的几天里,我一直试图修复这个运行时错误,但都没有用,我不知道为什么它在调用getDirection方法时创建节点列表时会抛出NullPointerException。我对Android编程非常陌生,如果有人能给我一些指导,我将不胜感激,谢谢 编辑:通过调试器判断,LatLng坐标和方向类型正在通过getDocument()方法传递,但文档没有从getDocument()方法返回Java 使用GMapsV2Direction类时出现NullPointerException,java,android,google-maps-android-api-2,Java,Android,Google Maps Android Api 2,在过去的几天里,我一直试图修复这个运行时错误,但都没有用,我不知道为什么它在调用getDirection方法时创建节点列表时会抛出NullPointerException。我对Android编程非常陌生,如果有人能给我一些指导,我将不胜感激,谢谢 编辑:通过调试器判断,LatLng坐标和方向类型正在通过getDocument()方法传递,但文档没有从getDocument()方法返回 nl1=doc.getElementsByTagName(“步骤”);对不起,我应该说清楚的!您是否尝试单步执
nl1=doc.getElementsByTagName(“步骤”);对不起,我应该说清楚的!您是否尝试单步执行调试器并检查doc的值?
doc
param对于类似的NPE为null,因此错误出现在调用此函数的代码中。添加一些创建文档
并调用getDirection
的代码。我添加了正在使用的函数,该函数调用getDirection方法。你知道getDocument()方法为什么不返回文档吗?
public Document getDocument(LatLng start, LatLng end, String mode) {
String url = "http://maps.googleapis.com/maps/api/directions/xml?"
+ "origin=" + start.latitude + "," + start.longitude
+ "&destination=" + end.latitude + "," + end.longitude
+ "&sensor=false&units=metric&mode=driving";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public ArrayList<LatLng> getDirection (Document doc) {
NodeList nl1, nl2, nl3, nl4;
ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
double time=0;
nl1 = doc.getElementsByTagName("step");
if (nl1.getLength() > 0) {
for (int i = 0; i < nl1.getLength(); i++) {
double t=0;
Node node1 = nl1.item(i);
nl2 = node1.getChildNodes();
Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
nl3 = locationNode.getChildNodes();
Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
double lat = Double.parseDouble(latNode.getTextContent());
Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
double lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "points"));
ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
for(int j = 0 ; j < arr.size() ; j++) {
listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
}
locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "lat"));
lat = Double.parseDouble(latNode.getTextContent());
lngNode = nl3.item(getNodeIndex(nl3, "lng"));
lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
locationNode = nl2.item(getNodeIndex(nl2, "duration"));
nl4 = locationNode.getChildNodes();
Node node2 = nl4.item(getNodeIndex(nl4, "value"));
t = Double.parseDouble(node2.getTextContent());
time=time+t;
}
Log.i("duration", String.valueOf(time));
}
return listGeopoints;
}
public void onClick_GetDirections(View v){
MyGPSToolDirections md = new MyGPSToolDirections();
LatLng fromPosition = new LatLng(13.68714, 100.53525);
LatLng toPosition = new LatLng(13.68366, 100.53900);
Document doc1 = md.getDocument(fromPosition, toPosition, MyGPSToolDirections.MODE_DRIVING);
ArrayList<LatLng> directionPoint = md.getDirection(doc1);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
GPSToolMap.addPolyline(rectLine);
}
08-07 17:14:31.682: E/AndroidRuntime(2201): FATAL EXCEPTION: main
08-07 17:14:31.682: E/AndroidRuntime(2201): java.lang.IllegalStateException: Could not execute method of the activity
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.view.View$1.onClick(View.java:3599)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.view.View.performClick(View.java:4204)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.view.View$PerformClick.run(View.java:17355)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.os.Handler.handleCallback(Handler.java:725)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.os.Looper.loop(Looper.java:137)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 17:14:31.682: E/AndroidRuntime(2201): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 17:14:31.682: E/AndroidRuntime(2201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 17:14:31.682: E/AndroidRuntime(2201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 17:14:31.682: E/AndroidRuntime(2201): at dalvik.system.NativeStart.main(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201): Caused by: java.lang.reflect.InvocationTargetException
08-07 17:14:31.682: E/AndroidRuntime(2201): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201): at java.lang.reflect.Method.invoke(Method.java:511)
08 -07 17:14:31.682: E/AndroidRuntime(2201): at android.view.View$1.onClick(View.java:3594)
08-07 17:14:31.682: E/AndroidRuntime(2201): ... 11 more
08-07 17:14:31.682: E/AndroidRuntime(2201): Caused by: java.lang.NullPointerException
08-07 17:14:31.682: E/AndroidRuntime(2201): at com.L00081183.mygpstool.MyGPSToolDirections.getDirection(MyGPSToolDirections.java:54)
08-07 17:14:31.682: E/AndroidRuntime(2201): at com.L00081183.mygpstool.MainActivity.onClick_GetDirections(MainActivity.java:145)