2013-07-31 35 views
0

我面臨的一個小問題,在我的代碼和卡住由於it.Following是我的代碼: -如何獲得lat和長在Android的

public class MainActivity extends Activity { 
TextView textView1; 
Location currentLocation; 
double currentLatitude,currentLongitude; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    textView1 = (TextView) findViewById(R.id.textView1); 

    findLocation(); 

    textView1.setText(String.valueOf(currentLatitude) + "\n" 
      + String.valueOf(currentLongitude)); 

} 

public void findLocation() { 

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

     LocationListener locationListener = new LocationListener() { 

      public void onLocationChanged(Location location) { 

       updateLocation(location,currentLatitude,currentLongitude); 

       Toast.makeText(
         MainActivity.this, 
         String.valueOf(currentLatitude) + "\n" 
           + String.valueOf(currentLongitude), 5000) 
         .show(); 

       } 

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

      public void onProviderEnabled(String provider) { 
      } 

      public void onProviderDisabled(String provider) { 
      } 
     }; 

     locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

    } 


    void updateLocation(Location location,double currentLatitude,double currentLongitude) { 
      currentLocation = location; 
      this.currentLatitude = currentLocation.getLatitude(); 
      this.currentLongitude = currentLocation.getLongitude(); 

     } 
} 

每一件事情正在fine.But我的問題是即類級別變量currentLatitude和currentLongitude的值爲null。在上面的代碼中,當我在更新位置方法中設置lat和long文本視圖時,它工作正常,但是當我想在文本視圖中設置相同的值時創建方法它給null值。爲什麼我不知道。請幫助解決這個問題。提前感謝!

+0

設置TextView的值'updateLocation' - 在'onCreate'的位置一直沒有找到尚未 –

+0

這需要一些時間來更新location.So在TextView中設置LLAT和長期內更新位置() –

+0

@BLaZuRE因爲'findLocation();'在被放入'textView1.set ...'前被調用。 – g00dy

回答

1

它是因爲當你在文本中設置文本在Oncreate方法lat和long沒有初始化。它會在更新時被初始化。

所以你應該在updatelocation()方法中設置文本。

locationlistener需要一些時間來更新它的經度和長度,這樣你的oncreate方法就會同時執行,這樣你的緯度和長度不會更新並保持爲空。所以更好地設置updatelocation。

希望它有助於!

+0

謝謝兄弟!但我希望這些類級別的變量可以在另一個類中使用。我該怎麼做? –

+0

在另一個類中使用變量意味着你想要在另一個類中使用值? –

+0

是的。其實我想創建這個類的對象,使其成爲一個簡單的Java類,並希望在另一個類中使用這些變量。 –

0

我建議你把textView1.setText(String.valueOf(currentLatitude) + "\n" + String.valueOf(currentLongitude));放在updateLocation函數中。

0

查找位置在返回值之前需要一些時間。它異步發生,同時你的UI線程繼續。

所以在onCreate(),你還沒有位置。這與onCreate()中的呼叫順序無關。

您只有當您的updateLocation()方法被調用時獲取位置,並且無法保證何時發生與您的視圖相關的設置。

所以,要解決,更新你的textView文本。

void updateLocation(Location location,double currentLatitude,double currentLongitude) { 
    currentLocation = location; 
    this.currentLatitude = currentLocation.getLatitude(); 
    this.currentLongitude = currentLocation.getLongitude(); 
    textView1.setText(String.valueOf(currentLatitude) + "\n" 
     + String.valueOf(currentLongitude)); 
}