2012-10-18 29 views
0

下面的代碼有什麼問題。它在這條線上拋出空指針異常(location.setLatitude(latitude);)。我試圖在應用程序啓動後立即創建一個圓,這是我從onCreate方法傳遞位置對象的原因。GeoPoint中的空指針異常

private Location location; 

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

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

    locationListener = new GPSLocationListener(); 

    locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 
      0, 
      0, 
      locationListener); 
    mapView = (MapView) findViewById(R.id.mapView); 
    listView = (ListView) findViewById(R.id.mylist); 
    mapView.setStreetView(true); 
    mapView.setBuiltInZoomControls(true); 

    mapController = mapView.getController(); 
    mapController.setZoom(15); 

    GeoPoint initialPoint = new GeoPoint((int) (36.778261* 1E6), (int) (-119.417932 * 1E6)); 
    double latitude = initialPoint .getLatitudeE6()/1E6; 
    double longitude = initialPoint .getLongitudeE6()/1E6; 

    location.setLatitude(latitude); 
    location.setLongitude(longitude); 
    locationListener.onLocationChanged(location); 
} 

以下是關注LocationUpdate的類。

private class GPSLocationListener implements LocationListener { 

    MapOverlay mapOverlay; 

    public GPSLocationListener(MapView mapView) { 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      GeoPoint point = new GeoPoint(
        (int) (location.getLatitude() * 1E6), 
        (int) (location.getLongitude() * 1E6)); 

      mapController.animateTo(point); 
      mapController.setZoom(15); 

      if (mapOverlay == null) { 
       mapOverlay = new MapOverlay(this,android.R.drawable.star_on); 
       List<Overlay> listOfOverlays = mapView.getOverlays(); 
       listOfOverlays.add(mapOverlay); 
      } 
      mapOverlay.setPointToDraw(point); 
      mapView.invalidate(); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

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

任何建議都會有很大的幫助。

更新: -

更改後我得到

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 

下面是我的代碼,我所做的更改。

GeoPoint initialPoint = new GeoPoint((int) (36.778261), (int) (-119.417932)); 
double latitude = initialPoint .getLatitudeE6()/1E6; 
double longitude = initialPoint .getLongitudeE6()/1E6; 

this.location = new Location("TechGeeky Randomly Initialized"); 
location.setLatitude(latitude); 
location.setLongitude(longitude); 
locationListener.onLocationChanged(location); 

這裏有什麼問題嗎?

+4

您沒有傳遞任何位置的物體在這片碼。你在別的地方設置位置嗎?如果不是的話,這很明顯爲什麼location == null。也許你只是忘了向LocationManager詢問最後一個已知的位置並設置它的位置? – Tobold

+1

位置'在給定的代碼段中從不初始化。初始化它。 –

+0

我在我的問題的代碼中添加了更多信息。我不確定我完全理解你們的意見。我的目標是在應用程序啓動後畫一個Circle,這就是我從'onCreate方法'傳遞初始位置的原因。任何人都可以從代碼的角度來解釋我。 – ferhan

回答

1

我認爲你想要做的不是從LocationManager獲取最後一個已知位置,而是從onCreate()中定義你自己的位置開始(例如一個隨機的位置)。要做到這一點,只需創建一個新Location對象,像這樣:

this.location = new Location("TechGeeky Randomly Initialized"); 

不要爲了這些線路在此之前實例化,以避免例外:

location.setLatitude(latitude); 
location.setLongitude(longitude); 
locationListener.onLocationChanged(location); 
+0

感謝Tobold,這就是我需要的。但在對代碼進行更改之後。它是''IndexOutOfBoundsException''你可以看看它爲什麼會發生?我更新了我的問題。 – ferhan

+0

@TechGeeky拋出異常在哪裏? – Tobold

0
使用此代碼

Intialize Location對象: -

如果要使用移動網絡將這個代碼的onCreate

Location location=null; 
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

如果你想在的onCreate

使用GPS將這個代碼獲得位置獲得位置
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
+0

我剛剛嘗試過,但它沒有在任何位置繪製任何圓圈。它看起來像位置是NULL。有什麼辦法可以通過在onCreate()方法本身中傳遞一些隨機位置來畫一個圓。我已經在問題中更新了我的代碼以提供清晰的圖像。 – ferhan

0

您已經實例化了位置對象。你在哪裏創建對象的位置。請使用創建的對象到位置:

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

通位置對象方法作爲參數,並檢查空狀態。