2010-08-20 241 views
-1

我有以下MapOverlay。下面一行是拋出一個空指針,但我不明白爲什麼:S mapView.getProjection().toPixels(p, screenPts);任何人都可以幫我嗎?空指針異常

public class Mapview extends MapActivity { 
GeoPoint p; 
MapView mapView; 
MapController mc; 

@Override 
protected boolean isRouteDisplayed() { 
return false; 
} 
public int icount = 0; 
MapController mapController; 
MyPositionOverlay positionOverlay; 
private LocationData tweets; 
public int start = 0; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 



MapView myMapView = (MapView)findViewById(R.id.mapView); 

myMapView.setSatellite(true); 
myMapView.setStreetView(true); 
myMapView.displayZoomControls(false); 
mapController = myMapView.getController(); 

mapController.setZoom(17); 
myMapView.setBuiltInZoomControls(true); 

//Adding points here 


//---Add a location marker--- 
MapOverlay mapOverlay = new MapOverlay(); 
List<Overlay> listOfOverlays = myMapView.getOverlays(); 
listOfOverlays.clear(); 
listOfOverlays.add(mapOverlay);   

myMapView.invalidate(); 

// Add the MyPositionOverlay 
positionOverlay = new MyPositionOverlay(); 
List<Overlay> overlays = myMapView.getOverlays(); 
overlays.add(positionOverlay); 


LocationManager locationManager; 
String context = Context.LOCATION_SERVICE; 
locationManager = (LocationManager)getSystemService(context); 

Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
criteria.setAltitudeRequired(false); 
criteria.setBearingRequired(false); 
criteria.setSpeedRequired(false); 
criteria.setCostAllowed(true); 
String provider = locationManager.getBestProvider(criteria, true); 


locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 
Location location = locationManager.getLastKnownLocation(provider); 
updateWithNewLocation(location); 

} 


private final LocationListener locationListener = new LocationListener() { 
public void onLocationChanged(Location location) { 
    updateWithNewLocation(location); 
} 

public void onProviderDisabled(String provider){ 
    updateWithNewLocation(null); 
} 

public void onProviderEnabled(String provider){ } 
public void onStatusChanged(String provider, int status, 
          Bundle extras){ } 
}; 

private void updateWithNewLocation(Location location) { 
String latLongString =""; 
TextView myLocationText; 
myLocationText = (TextView)findViewById(R.id.myLocationText); 
String addressString = "No address found"; 

if (location != null) { 
    // Update my location marker 
    positionOverlay.setLocation(location); 

    // Update the map location. 
    Double geoLat = location.getLatitude()*1E6; 
    Double geoLng = location.getLongitude()*1E6; 
    GeoPoint point = new GeoPoint(geoLat.intValue(), 
           geoLng.intValue()); 

    mapController.animateTo(point); 

    double lat = location.getLatitude(); 
    double lng = location.getLongitude(); 
    latLongString = "Lat:" + lat + "\nLong:" + lng; 
    Global.lat = lat; 
    Global.lng = lng; 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 

    Geocoder gc = new Geocoder(this, Locale.getDefault()); 
    try { 
    List<Address> addresses = gc.getFromLocation(latitude, 
               longitude, 1); 
    StringBuilder sb = new StringBuilder(); 
    if (addresses.size() > 0) { 
     Address address = addresses.get(0); 

     for (int i = 0; i < address.getMaxAddressLineIndex(); i++) 
     sb.append(address.getAddressLine(i)).append("\n"); 

     sb.append(address.getLocality()).append("\n"); 
     sb.append(address.getPostalCode()).append("\n"); 
     sb.append(address.getCountryName()); 
    } 
    addressString = sb.toString(); 
    } catch (IOException e) {} 
} else { 
    latLongString = "No location found"; 
} 
} 

class MapOverlay extends com.google.android.maps.Overlay 
{ 
    @Override 
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    { 
     super.draw(canvas, mapView, shadow);     

     //---translate the GeoPoint to screen pixels--- 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(p, screenPts); 

     //---add the marker--- 
     Bitmap bmp = BitmapFactory.decodeResource(
      getResources(), R.drawable.marker);    
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   
     return true; 
} 

} 
} 
+0

我看不到,'p'來自哪裏...... – WarrenFaith 2010-08-20 10:55:46

+0

它早些時候被聲明爲'Geopoint p;'。我添加了更多的代碼來使這個更清晰的抱歉。 – Skizit 2010-08-20 10:59:21

+0

謝謝。我不能的錯誤,你找出哪個對象是空的? – WarrenFaith 2010-08-20 11:06:54

回答

2

我看到的GeoPoint p的聲明,但沒有表達,其中p實際上初始化。因此,將null傳遞給toPixels()方法可能是NPE的一個原因。

試試這個代碼,而不是來檢查,如果NPE消失:

//---translate the GeoPoint to screen pixels--- 
    if (p == null) return false; 
    Point screenPts = mapView.getProjection().toPixels(p, null); 

(我不喜歡「出」在Java中的參數,toPixels將創建點對象爲你 並返回它)

+0

你的代碼幫助我進一步調試它。我能夠解決餘下的問題。基本上我沒有提供一點:)謝謝。 – Skizit 2010-08-20 11:49:11

+0

這就是我的預期。 NPE在這個地方唯一的其他原因應該是一個'null'而不是一個MapView或一個'null'而不是一個投影,其中一個案例會是android上的一個嚴重錯誤;) – 2010-08-20 11:56:24

0

好吧,似乎公共屬性GeoPoint p從來沒有被引用 因此pointint null。 (), geoLng.intValue());或者你想在這一行中定義p:

您應該找到要分配p的corredt位置,然後問題就解決了。

0

您需要保留的myMapView,你將在以後使用的參考。做到這一點的方法是使用strong屬性將其聲明爲屬性。