-1

我为什么要这样做?应用程序运行得很好,当我决定使用 tracaRota 方法来构建从 uma ponit 到另一个的路线时,它开始这样做并且应用程序甚至没有启动

11-30 10:45:28.836: E/AndroidRuntime(14114): FATAL EXCEPTION: main
11-30 10:45:28.836: E/AndroidRuntime(14114): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aplicacaoteste1/com.example.aplicacaoteste1.Main}: java.lang.NullPointerException
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.os.Looper.loop(Looper.java:123)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at java.lang.reflect.Method.invokeNative(Native Method)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at java.lang.reflect.Method.invoke(Method.java:521)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at dalvik.system.NativeStart.main(Native Method)
11-30 10:45:28.836: E/AndroidRuntime(14114): Caused by: java.lang.NullPointerException
11-30 10:45:28.836: E/AndroidRuntime(14114):    at com.example.aplicacaoteste1.Main.tracaRota(Main.java:167)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at com.example.aplicacaoteste1.Main.onCreate(Main.java:77)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-30 10:45:28.836: E/AndroidRuntime(14114):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-30 10:45:28.836: E/AndroidRuntime(14114):    ... 11 more


    package com.example.aplicacaoteste1;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Main extends MapActivity {

private MapController mControl;
private MapView mapV;
private LocationManager lManager;
private MyLocationOverlay myLOverlay;
private MyOverlays itemizedOverlay;


@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_main);

    mapV = (MapView) findViewById(R.id.mapView);
    //mapV.displayZoomControls(true);
    mapV.setBuiltInZoomControls(true);
    //mapV.setSatellite(true);

    mControl = mapV.getController();
    mControl.setZoom(5);

    lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler());

    myLOverlay = new MyLocationOverlay(this, mapV);
    mapV.getOverlays().add(myLOverlay);


    List<Overlay> mapOverlays = mapV.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    PontoTurísticos pontoTuristico = new PontoTurísticos(drawable, this);

    GeoPoint point1 = new GeoPoint(19240000,-99120000);
    OverlayItem novoPontoTuristico = new OverlayItem(point1, "Hola, Mundo!", "I'm in Mexico City!");

    GeoPoint point2 = new GeoPoint(-80535835, -348788191);
    OverlayItem novoPontoTuristico2 = new OverlayItem(point2, "SJCC!", "Olá, tudo bem?!");

    pontoTuristico.addOverlay(novoPontoTuristico);
    pontoTuristico.addOverlay(novoPontoTuristico2);
    mapOverlays.add(pontoTuristico);

    tracaRota(myLOverlay.getMyLocation(), novoPontoTuristico2.getPoint(), Color.GREEN, mapV);

    myLOverlay.runOnFirstFix(new Runnable() {

        @Override
        public void run() {
            mapV.getController().animateTo(myLOverlay.getMyLocation());

        }
    });

    //Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
    //itemizedOverlay = new MyOverlays(this, drawable);
    //createMarker();



//      double lat = -8.0556;
//      double longi = 34.8911;
}


public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

protected boolean isRouteDisplayed() {
    return false;
}

public class GeoUpdateHandler implements LocationListener{

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int log = (int) (location.getLongitude());

        GeoPoint point = new GeoPoint(lat, log);
        //createMarker();
        //mControl.animateTo(point); 
        mControl.setCenter(point);

    }

    @Override
    public void onProviderDisabled(String arg0) {

    }

    @Override
    public void onProviderEnabled(String arg0) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

}

//private void createMarker(){
//  GeoPoint p  = mapV.getMapCenter();
//  OverlayItem overlayitem = new OverlayItem(p, "", "");
//  itemizedOverlay.addOverlay(overlayitem);
//  if(itemizedOverlay.size() < 0){
//      mapV.getOverlays().add(itemizedOverlay);
//  }
//}

protected void onResume(){
    super.onResume();
    myLOverlay.enableMyLocation();
    myLOverlay.enableCompass();
}

protected void onPause(){
    super.onPause();
    myLOverlay.disableMyLocation();
    myLOverlay.disableCompass();
}

private void tracaRota(GeoPoint src,GeoPoint dest, int color, MapView mV) 
{ 
    // connect to map web service 
    StringBuilder urlString = new StringBuilder(); 
    urlString.append("http://maps.google.com/maps?f=d&hl=en"); 
    urlString.append("&saddr=");//from 
    urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 )); 
    urlString.append(","); 
    urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 )); 
    urlString.append("&daddr=");//to 
    urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 )); 
    urlString.append(","); 
    urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 )); 
    urlString.append("&ie=UTF8&0&om=0&output=kml"); 
    //Log.d("xxx","URL="+urlString.toString()); 
    // get the kml (XML) doc. And parse it to get the coordinates(direction route). 
    Document doc = null; 
    HttpURLConnection urlConnection= null; 
    URL url = null; 
    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()); 

        if(doc.getElementsByTagName("GeometryCollection").getLength()>0) 
        { 
            //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName(); 
            String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ; 
            //Log.d("xxx","path="+ path); 
            String [] pairs = path.split(" "); 
            String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height 
            // src 
            GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6)); 
            mV.getOverlays().add(new pontosRota(startGP,startGP,1)); 
            GeoPoint gp1; 
            GeoPoint gp2 = startGP; 
            for(int i=1;i<pairs.length;i++) // the last one would be crash 
            { 
                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)); 
                mV.getOverlays().add(new pontosRota(gp1,gp2,2,color)); 
                //Log.d("xxx","pair:" + pairs[i]); 
            } 
            mV.getOverlays().add(new pontosRota(dest,dest, 3)); // use the default color 
        } 
    } 
    catch (MalformedURLException e) 
    { 
        e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
        e.printStackTrace(); 
    } 
    catch (ParserConfigurationException e) 
    { 
        e.printStackTrace(); 
    } 
    catch (SAXException e) 
    { 
        e.printStackTrace(); 
    } 
}

}
4

1 回答 1

0

似乎在这条线上:

tracaRota(myLOverlay.getMyLocation(), novoPontoTuristico2.getPoint(), Color.GREEN, mapV);

mLOverlay.getMyLocation返回空值。

尝试将其移入myLOverlay.runOnFirstFix runner

于 2012-11-30T17:30:11.230 回答