Java 安卓:获得纬度&;其他类中的经度,但该值变为0.0
我有两门课。。First.java和Second.java 在头等舱。。纬度、经度和地址在TextView中正确显示。 但在第二类中,纬度和经度变为0.0,地址变为空 有人能帮我吗?为什么 下面是First.java的代码Java 安卓:获得纬度&;其他类中的经度,但该值变为0.0,java,android,geolocation,latitude-longitude,Java,Android,Geolocation,Latitude Longitude,我有两门课。。First.java和Second.java 在头等舱。。纬度、经度和地址在TextView中正确显示。 但在第二类中,纬度和经度变为0.0,地址变为空 有人能帮我吗?为什么 下面是First.java的代码 public class First extends Activity { private Context context=null; AppLocationService appLocationService; String address; double latitu
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;
}
在您的第一个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;
}
在适当的位置(比如你从哪里得到lat/long/address),添加这些行
latitude = nwLocation.getLatitude();
setLatitude (latitude);
longitude = nwLocation.getLongitude();
setLongitude (longitude);
在您的第一个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;
}
在适当的位置(比如你从哪里得到lat/long/address),添加这些行
latitude = nwLocation.getLatitude();
setLatitude (latitude);
longitude = nwLocation.getLongitude();
setLongitude (longitude);
在您的第一个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;
}
在适当的位置(比如你从哪里得到lat/long/address),添加这些行
latitude = nwLocation.getLatitude();
setLatitude (latitude);
longitude = nwLocation.getLongitude();
setLongitude (longitude);
在您的第一个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;
}
在适当的位置(比如你从哪里得到lat/long/address),添加这些行
latitude = nwLocation.getLatitude();
setLatitude (latitude);
longitude = nwLocation.getLongitude();
setLongitude (longitude);
你只获得了价值。这意味着你已经编写了getter方法。您尚未使用setter方法设置值。请试试看
public void setLatitude(double latitude){
this.latitude = latitude;
}
public void setLongitude(double longitude){
this.longitude=longitude;
}
public setAlamat(String address){
this.address=address;
}
你只获得了价值。这意味着你已经编写了getter方法。您尚未使用setter方法设置值。请试试看
public void setLatitude(double latitude){
this.latitude = latitude;
}
public void setLongitude(double longitude){
this.longitude=longitude;
}
public setAlamat(String address){
this.address=address;
}
你只获得了价值。这意味着你已经编写了getter方法。您尚未使用setter方法设置值。请试试看
public void setLatitude(double latitude){
this.latitude = latitude;
}
public void setLongitude(double longitude){
this.longitude=longitude;
}
public setAlamat(String address){
this.address=address;
}
你只获得了价值。这意味着你已经编写了getter方法。您尚未使用setter方法设置值。请试试看
public void setLatitude(double latitude){
this.latitude = latitude;
}
public void setLongitude(double longitude){
this.longitude=longitude;
}
public setAlamat(String address){
this.address=address;
}
您正在初始化一个新实例data_dr_location=new AddLocation();得到位置,纬度和经度,所以它显然是0。实际上,您需要使用intent将数据从class1传递到class2。我想这两门课都是活动课。@RajenRaiyarela:谢谢。。是的,这两门课都是活动课。但是当我删除数据时_dr_location=new AddLocation(),二等舱坠毁了。还有什么不对吗?@NishanthiGrashia我没有找到标记。很抱歉我是新来的。@vhiefa。。。正如我所写,您需要将数据从一个活动传递到另一个活动。另外,如果删除初始化代码,那么当前实例显然为null且未初始化,因此将给出NPE并将崩溃,因为我看到您没有使用任何try-catch。pl检查此链接,了解如何将类对象从一个活动传递到另一个活动。正如其他人提到的,您需要添加setter方法来设置变量的值。@RajenRaiyarela感谢链接。我遵循了它,它就像一个符咒您正在初始化一个新实例数据\u dr_location=new AddLocation();得到位置,纬度和经度,所以它显然是0。实际上,您需要使用intent将数据从class1传递到class2。我想这两门课都是活动课。@RajenRaiyarela:谢谢。。是的,这两门课都是活动课。但是当我删除数据时_dr_location=new AddLocation(),二等舱坠毁了。还有什么不对吗?@NishanthiGrashia我没有找到标记。很抱歉我是新来的。@vhiefa。。。正如我所写,您需要将数据从一个活动传递到另一个活动。另外,如果删除初始化代码,那么当前实例显然为null且未初始化,因此将给出NPE并将崩溃,因为我看到您没有使用任何try-catch。pl检查此链接,了解如何将类对象从一个活动传递到另一个活动。正如其他人提到的,您需要添加setter方法来设置变量的值。@RajenRaiyarela感谢链接。我遵循了它,它就像一个符咒您正在初始化一个新实例数据\u dr_location=new AddLocation();得到位置,纬度和经度,所以它显然是0。实际上,您需要使用intent将数据从class1传递到class2。我想这两门课都是活动课。@RajenRaiyarela:谢谢。。是的,这两门课都是活动课。但是当我删除数据时_dr_location=new AddLocation(),二等舱坠毁了。还有什么不对吗?@NishanthiGrashia我没有找到标记。很抱歉我是新来的。@vhiefa。。。正如我所写,您需要将数据从一个活动传递到另一个活动。另外,如果删除初始化代码,那么当前实例显然为null且未初始化,因此将给出NPE并将崩溃,因为我看到您没有使用任何try-catch。pl检查此链接,了解如何将类对象从一个活动传递到另一个活动。正如其他人提到的,您需要添加setter方法来设置变量的值。@RajenRaiyarela感谢链接。我遵循了它,它就像一个符咒您正在初始化一个新实例数据\u dr_location=new AddLocation();得到位置,纬度和经度,所以它显然是0。实际上,您需要使用intent将数据从class1传递到class2。我想这两门课都是活动课。@RajenRaiyarela:谢谢。。是的,这两门课都是活动课。但是当我删除数据时_dr_location=new AddLocation(),二等舱坠毁了。还有什么不对吗?@NishanthiGrashia我没有找到标记。很抱歉我是新来的。@vhiefa。。。正如我所写,您需要将数据从一个活动传递到另一个活动。另外,如果删除初始化代码,那么当前实例显然为null且未初始化,因此将给出NPE并将崩溃,因为我看到您没有使用任何try-catch。pl检查此链接,了解如何将类对象从一个活动传递到另一个活动。正如其他人提到的,您需要添加setter方法来设置变量的值。@RajenRaiyarela感谢链接。我遵循了它,它就像一个符咒D@vhiefa:请接受答案,确认答案。单击答案旁边的灰色记号以接受为答案。@vhiefa:在该答案旁边,您将找到一个灰色记号。您必须检查它,并将其设置为绿色以接受作为答案。如果你接受了一份工作,你将得到+2分answer@vhiefa:请交流