我面臨的一個小問題,在我的代碼和卡住由於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值。爲什麼我不知道。請幫助解決這個問題。提前感謝!
設置TextView的值'updateLocation' - 在'onCreate'的位置一直沒有找到尚未 –
這需要一些時間來更新location.So在TextView中設置LLAT和長期內更新位置() –
@BLaZuRE因爲'findLocation();'在被放入'textView1.set ...'前被調用。 – g00dy