google maps - NullPointerException when drawing getting path in android -
i'm trying ask google path between 2 geographic points draw. have nullpointerexception.
here, call method makes peticion:
latlng start = new latlng(13.6871, 100.5352); latlng end = new latlng(13.6836, 100.5390); stringbuilder sbdoc = null; try { sbdoc = getdocument(start, end, mode_driving); if (sbdoc != null){ string doc = sbdoc.tostring(); }else {log.e("the route of line null","");} } catch (ioexception e) { e.printstacktrace(); }
and method:
public stringbuilder getdocument(latlng start, latlng end, string mode) throws ioexception { string url = "http://maps.googleapis.com/maps/api/directions/json?" + "origin=" + start.latitude + "," + start.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=driving"; stringbuilder builder = null; url categorieswsurl = new url(url); urlconnection connection = categorieswsurl.openconnection(); string line; builder = new stringbuilder(); inputstream response = connection.getinputstream(); inputstreamreader isr = new inputstreamreader( response ); bufferedreader reader = new bufferedreader(isr); while((line = reader.readline()) != null){ builder.append(line + "\n"); } return builder; }
the stractrace of logcat:
05-04 14:29:43.550: e/androidruntime(11689): fatal exception: main 05-04 14:29:43.550: e/androidruntime(11689): java.lang.runtimeexception: unable start activity componentinfo{package/package.map}: android.os.networkonmainthreadexception 05-04 14:29:43.550: e/androidruntime(11689): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 05-04 14:29:43.550: e/androidruntime(11689): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 05-04 14:29:43.550: e/androidruntime(11689): @ android.app.activitythread.access$600(activitythread.java:141) 05-04 14:29:43.550: e/androidruntime(11689): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 05-04 14:29:43.550: e/androidruntime(11689): @ android.os.handler.dispatchmessage(handler.java:99) 05-04 14:29:43.550: e/androidruntime(11689): @ android.os.looper.loop(looper.java:137) 05-04 14:29:43.550: e/androidruntime(11689): @ android.app.activitythread.main(activitythread.java:5041) 05-04 14:29:43.550: e/androidruntime(11689): @ java.lang.reflect.method.invokenative(native method) 05-04 14:29:43.550: e/androidruntime(11689): @ java.lang.reflect.method.invoke(method.java:511) 05-04 14:29:43.550: e/androidruntime(11689): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-04 14:29:43.550: e/androidruntime(11689): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-04 14:29:43.550: e/androidruntime(11689): @ dalvik.system.nativestart.main(native method) 05-04 14:29:43.550: e/androidruntime(11689): caused by: android.os.networkonmainthreadexception 05-04 14:29:43.550: e/androidruntime(11689): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 05-04 14:29:43.550: e/androidruntime(11689): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 05-04 14:29:43.550: e/androidruntime(11689): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 05-04 14:29:43.550: e/androidruntime(11689): @ java.net.inetaddress.getallbyname(inetaddress.java:214) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpconnection.<init>(httpconnection.java:70) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpconnection.<init>(httpconnection.java:50) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpconnection$address.connect(httpconnection.java:340) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpconnectionpool.get(httpconnectionpool.java:87) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpconnection.connect(httpconnection.java:128) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpengine.opensocketconnection(httpengine.java:316) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpengine.connect(httpengine.java:311) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpengine.sendsocketrequest(httpengine.java:290) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpengine.sendrequest(httpengine.java:240) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:282) 05-04 14:29:43.550: e/androidruntime(11689): @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:177) 05-04 14:29:43.550: e/androidruntime(11689): @ package.map.getdocument(map.java:110) 05-04 14:29:43.550: e/androidruntime(11689): @ package.map.oncreate(map.java:84) 05-04 14:29:43.550: e/androidruntime(11689): @ android.app.activity.performcreate(activity.java:5104) 05-04 14:29:43.550: e/androidruntime(11689): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 05-04 14:29:43.550: e/androidruntime(11689): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144)
networkonmainthreadexception
error because have run network related operations in background thread. use asynctask
, write code in doinbackground()
.
Comments
Post a Comment