ANDROID:在其他类中获取纬度和经度,但值变为0.0
Posted
技术标签:
【中文标题】ANDROID:在其他类中获取纬度和经度,但值变为0.0【英文标题】:ANDROID: getting latitude&longitude in other class, but the value become 0.0 【发布时间】:2014-08-05 05:29:21 【问题描述】:我有两个类.. First.java 和 Second.java,
在 First class.. 纬度、经度和地址在 TextView 中正确显示。 但是在第二类中,经纬度变为 0.0 ,地址变为空。
谁能帮我解释一下为什么?
这是 First.java 的代码
public class First extends Activity
private Context context=null;
AppLocationService appLocationService;
String address;
double latitude;
double longitude;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.add_location);
context=this;
appLocationService = new AppLocationService(First.this);
btngetLocation=(Button)findViewById(R.id.button_getLocation);
btngetLocation.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View arg0)
// getting GPS status
boolean isGPSEnabled = appLocationService
.getStatus(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = appLocationService
.getStatus(LocationManager.NETWORK_PROVIDER);
if( isGPSEnabled==false && isNetworkEnabled==false)
showSettingsAlert("Location Service");
else
Toast.makeText(
getApplicationContext(),
"Attempt to get location...Please Wait.. ",
Toast.LENGTH_LONG).show();
if( isNetworkEnabled ==true )
Location nwLocation = appLocationService
.getLocation(LocationManager.NETWORK_PROVIDER);
if (nwLocation == null)
Toast.makeText(
getApplicationContext(),
"Network bermasalah.. Pastikan Anda terkoneksi Internet, dan coba get Location lagi",
Toast.LENGTH_LONG).show();
else
latitude = nwLocation.getLatitude();
longitude = nwLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (NW): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
else
Location gpsLocation = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation == null)
Toast.makeText(
getApplicationContext(),
"GPS bermasalah.. Silahkan di ruangan terbuka atau aktifkan network provider Anda, dan coba get Location lagi",
Toast.LENGTH_LONG).show();
else
latitude = gpsLocation.getLatitude();
longitude = gpsLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (GPS): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
);
public class AppLocationService extends Service implements LocationListener
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 10; //10 meter
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 3; //3 menit
public AppLocationService(Context context)
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
public boolean getStatus(String provider)
return locationManager.isProviderEnabled(provider); /
public Location getLocation(String provider)
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null)
location = locationManager.getLastKnownLocation(provider);
locationManager.removeUpdates(this);
return location;
return null;
@Override
public void onLocationChanged(Location location)
// TODO Auto-generated method stub
@Override
public void onProviderDisabled(String provider)
// TODO Auto-generated method stub
@Override
public void onProviderEnabled(String provider)
// TODO Auto-generated method stub
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
// TODO Auto-generated method stub
@Override
public IBinder onBind(Intent intent)
// TODO Auto-generated method stub
return null;
public String getAddress(Context ctx, double latitude, double longitude)
// StringBuilder result = new StringBuilder();
String alamat=null;
try
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
Address address = addresses.get(0);
String jalan = (address.getAddressLine(0));
String locality=address.getLocality();
String country=address.getCountryName();
String country_code=address.getCountryCode();
String zipcode=address.getPostalCode();
alamat = jalan +", "+ locality +", "+ country+", " + country_code+", " + zipcode;
catch (IOException e)
Log.e("tag", e.getMessage());
return alamat;
public double getLatitude()
return latitude;
public double getLongitude()
return longitude;
public String getAlamat()
return address;
这是第二个。爪哇
public class Second extends Activity
AddLocation data_dr_location;
Double latitude;
Double longitude;
String address;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.add_description);
data_dr_location = new AddLocation();
address= data_dr_location.getAlamat();
latitude = data_dr_location.getLatitude();
longitude = data_dr_location.getLongitude();
【问题讨论】:
您正在初始化一个新实例 data_dr_location = new AddLocation();并获取位置、纬度和经度,因此它显然是 0。您实际上需要使用意图将数据从 class1 传递到 class2。我假设这两个类都是活动类。 @RajenRaiyarela:谢谢.. 是的,两个课程都是活动课程。但是当我删除 data_dr_location = new AddLocation(); ,第二节课崩溃了。还有什么问题吗? @NishanthiGrashia 我没有找到标记。对不起..我是新来的。 @vhiefa ...正如我所写,您需要将数据从一个活动传递到另一个活动。此外,如果您删除初始化代码,那么显然当前实例为空且未初始化,因此会给出 NPE 并且会崩溃,因为我看到您没有使用任何 try catch。请检查此链接hmkcode.com/android-passing-java-object-another-activity 了解如何将类对象从一个活动传递到另一个活动。同样正如其他人提到的,您需要添加 setter 方法来设置变量的值。 @RajenRaiyarela 感谢您的链接。我跟着它,它就像一个魅力。 :D 【参考方案1】:在你的第一个java中,只有getter方法,没有setter方法!
private double latitude;
private double longitude;
private String address;
public double getLatitude()
return latitude;
public double getLongitude()
return longitude;
public String getAlamat()
return address;
/* THE BELOW SETTER METHODS ARE MISSING IN YOUR CODE.*/
public void setLatitude(double latitude)
this.latitude = latitude;
public void setLongitude(double longitude)
this.longitude=longitude;
public setAlamat(String address)
this.address=address;
在适当的位置(比如你得到纬度/经度/地址的任何地方),添加这些行。
latitude = nwLocation.getLatitude();
setLatitude (latitude);
longitude = nwLocation.getLongitude();
setLongitude (longitude);
【讨论】:
@vhiefa:请通过接受它作为答案来确认答案。单击答案旁边的灰色刻度线以接受作为答案。 @vhiefa:在这个答案旁边,你会发现一个灰色的勾号。您必须检查它并将其设为绿色以接受答案。接受答案您将获得 +2 分【参考方案2】:你只获得了价值。意味着你已经写了 getter mehtod。您尚未使用 setter 方法设置值。请尝试并检查。
public void setLatitude(double latitude)
this.latitude = latitude;
public void setLongitude(double longitude)
this.longitude=longitude;
public setAlamat(String address)
this.address=address;
【讨论】:
以上是关于ANDROID:在其他类中获取纬度和经度,但值变为0.0的主要内容,如果未能解决你的问题,请参考以下文章