1
當我開始onActivityResult我得到意圖和我的標記是在谷歌地圖上,但標記之間的路線沒有繪製,並在第二個或兩個我的程序崩潰後。我的代碼和日誌:使用谷歌地圖路線繪製應用程序崩潰
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1) {
if(resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
String result = extras.getString("linija");
setMarkers(result);
}
if(resultCode == RESULT_CANCELED) {
finish();
}
}
}
private void setMarkers(String linija) {
map.clear();
try {
ruta = SAXXMLParser.parse(getAssets().open("linije/linija" +linija+ ".xml"));
markerPoints.clear();
for (int i = 0; i < ruta.size(); i++) {
final LatLng lokacija = new LatLng(Double.parseDouble(ruta.get(
i).getLat()), Double.parseDouble(ruta.get(i).getLng()));
map.addMarker(new MarkerOptions()
.position(lokacija)
.title(ruta.get(i).getName())
.snippet(ruta.get(i).getAddress())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.busstop1)));
markerPoints.add(lokacija);
}
if(markerPoints.size() == ruta.size())
{
for(int j = 1; j < markerPoints.size(); j++) {
int i = j - 1;
LatLng origin = markerPoints.get(i);
LatLng dest = markerPoints.get(j);
// Getting URL to the Google Directions API
String url = getDirectionsUrl(origin, dest);
DownloadTask downloadTask = new DownloadTask();
// Start downloading json data from Google Directions API
downloadTask.execute(url);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public String getDirectionsUrl(LatLng origin,LatLng dest){
// Origin of route
String str_origin = "origin="+origin.latitude+","+origin.longitude;
// Destination of route
String str_dest = "destination="+dest.latitude+","+dest.longitude;
// Sensor enabled
String sensor = "sensor=false";
// Waypoints
String waypoints = "";
for(int i=2;i<markerPoints.size();i++){
LatLng point = (LatLng) markerPoints.get(i);
if(i==2)
waypoints = "waypoints=";
waypoints += point.latitude + "," + point.longitude + "|";
}
// Building the parameters to the web service
String parameters = str_origin+"&"+str_dest+"&"+sensor+"&"+waypoints;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;
return url;
}
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while((line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}
// Fetches data from url passed
public class DownloadTask extends AsyncTask<String, Void, String>{
// Downloading data in non-ui thread
@Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try{
// Fetching the data from web service
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
// Executes in UI thread, after the execution of
// doInBackground()
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{
// Parsing the data in non-ui thread
@Override
protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try{
jObject = new JSONObject(jsonData[0]);
DirectionsJSONParser parser = new DirectionsJSONParser();
// Starts parsing data
routes = parser.parse(jObject);
}catch(Exception e){
e.printStackTrace();
}
return routes;
}
// Executes in UI thread, after the parsing process
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points = null;
PolylineOptions lineOptions = null;
// Traversing through all the routes
for(int i=0;i<result.size();i++){
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(2);
lineOptions.color(Color.BLUE);
}
// Drawing polyline in the Google Map for the i-th route
map.addPolyline(lineOptions);
}
}
我只是複製/粘貼什麼,我認爲是非常重要的,所以也許一個支架丟失,也有日食沒有錯誤... logcat的:
08-28 22:03:57.451: E/AndroidRuntime(5248): FATAL EXCEPTION: main
08-28 22:03:57.451: E/AndroidRuntime(5248): java.lang.NullPointerException
08-28 22:03:57.451: E/AndroidRuntime(5248): at ova.<init>(Unknown Source)
08-28 22:03:57.451: E/AndroidRuntime(5248): at oux.b(Unknown Source)
08-28 22:03:57.451: E/AndroidRuntime(5248): at oux.a(Unknown Source)
08-28 22:03:57.451: E/AndroidRuntime(5248): at oyf.a(Unknown Source)
08-28 22:03:57.451: E/AndroidRuntime(5248): at grl.onTransact(SourceFile:137)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
android.os.Binder.transact(Binder.java:297)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addPolyline(Unknown Source)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
com.project.busme.MainActivity$ParserTask.onPostExecute(MainActivity.java:342)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
com.project.busme.MainActivity$ParserTask.onPostExecute(MainActivity.java:1)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
android.os.AsyncTask.finish(AsyncTask.java:602)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
android.os.AsyncTask.access$600(AsyncTask.java:156)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
android.os.Looper.loop(Looper.java:137)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
android.app.ActivityThread.main(ActivityThread.java:4424)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
java.lang.reflect.Method.invokeNative(Native Method)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
java.lang.reflect.Method.invoke(Method.java:511)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
08-28 22:03:57.451: E/AndroidRuntime(5248): at
dalvik.system.NativeStart.main(Native Method)
謝謝提前!