【发布时间】: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
标签: java android geolocation latitude-longitude