我是java编程的新手。我在这里遇到了一些问题,有人可以发表评论吗?当我签入onLocationChanged()
时。 lngpoint
和latpoint
具有期望值。但当我将其称为另一种方法时,loadcoordinate()
,loc.latpoint
和loc.lngpoint
将变为零?为什么呢?
编辑..基本上我的代码是这样的:
public class Testgpslocation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button gpsButton = (Button) findViewById(R.id.gpsButton);
LocationManager a = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
a.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new MyLocationListener());
gpsButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
LoadCoords();
}});
}
MyLocationListener loc;
public void LoadCoords(){
loc = new MyLocationListener();
String message = String.format(
"wao New Location \n Longitude: %1$s \n Latitude: %2$s",
loc.latpoint, loc.lngpoint
);
Toast.makeText(Testgpslocation.this, message, Toast.LENGTH_LONG).show();
TextView latText = (TextView) findViewById(R.id.latText);
TextView lngText = (TextView) findViewById(R.id.lngText);
double latitude = loc.latpoint;
double longtitude = loc.lngpoint;
latText.setText(Double.toString(latitude));
lngText.setText(Double.toString(longtitude));
}
在另一堂课:
private class MyLocationListener implements LocationListener {
private double latpoint;
private double lngpoint;
public void onLocationChanged(Location location) {
this.latpoint = location.getLatitude();
this.lngpoint = location.getLongitude();
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",latpoint, lngpoint
);
Toast.makeText(Testgpslocation.this, message, Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
答案 0 :(得分:1)
我相信LocationListener loc应该在类范围内定义,而不是在construtor中定义,就像这样
Public class A{
MyLocationListener loc;
public void loadcoordinate(){
loc = new MyLocationListener();
loc.latpoint, loc.lngpoint;
}
}
答案 1 :(得分:0)
我认为问题出在这一行:
MyLocationListener loc = new MyLocationListener();
loc.latpoint, loc.lngpoint;
你打算这样做吗?:
MyLocationListener loc = new MyLocationListener(loc.latpoint, loc.lngpoint);
另外,您是否打算在代码的第一行使用“Public”?这甚至是有效的
答案 2 :(得分:0)
请尝试以下代码:
public class Testgpslocation extends Activity {
/** Called when the activity is first created. */
MyLocationListener loc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button gpsButton = (Button) findViewById(R.id.gpsButton);
LocationManager a = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
loc = new MyLocationListener();
a.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
gpsButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
LoadCoords();
}});
}
public void LoadCoords(){
String message = String.format(
"wao New Location \n Longitude: %1$s \n Latitude: %2$s",
loc.latpoint, loc.lngpoint
);
Toast.makeText(Testgpslocation.this, message, Toast.LENGTH_LONG).show();
TextView latText = (TextView) findViewById(R.id.latText);
TextView lngText = (TextView) findViewById(R.id.lngText);
double latitude = loc.latpoint;
double longtitude = loc.lngpoint;
latText.setText(Double.toString(latitude));
lngText.setText(Double.toString(longtitude));
}