不能从静态上下文中引用非静态变量?
Non-static variable cannot be referenced from a static context?
我是Android编码的n00b,今天我想尝试使用定位服务。
我设置了一个简单的class和一个简单的main,只是为了有经度和纬度。
但是当我尝试调用结构来检索经度和纬度时 Android Studio 弹出错误:
"Error:(33, 16) error: non-static variable latitude cannot be referenced from a static context"
这是我的位置Class
public class AppLocationManager implements LocationListener {
private LocationManager locationManager;
private String latitude;
private String longitude;
private Criteria criteria;
private String provider;
public AppLocationManager(Context context) {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,0,this);
setMostRecentLocation(locationManager.getLastKnownLocation(provider));
}
private void setMostRecentLocation(Location lastKnownLocation) {
}
public String getLatitude() {
return latitude;
}
public String getLongitude() {
return longitude;
}
/*
* (non-Javadoc)
*
* @see
* android.location.LocationListener#onLocationChanged(android.location.
* Location)
*/
@Override
public void onLocationChanged(Location location) {
double lon = (double) (location.getLongitude());/// * 1E6);
double lat = (double) (location.getLatitude());// * 1E6);
// int lontitue = (int) lon;
// int latitute = (int) lat;
latitude = lat + "";
longitude = lon + "";
}
/*
* (non-Javadoc)
*
* @see
* android.location.LocationListener#onProviderDisabled(java.lang.String)
*/
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see
* android.location.LocationListener#onProviderEnabled(java.lang.String)
*/
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see android.location.LocationListener#onStatusChanged(java.lang.String,
* int, android.os.Bundle)
*/
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
要访问非静态属性和方法,您需要 class 的对象。要了解阅读 this so thread :
AppLocationManager appLocationManager=new AppLocationManager(context);
String latitude=appLocationManager.getLatitude();
String longitude=appLocationManager.getLongitude();
将您的变量声明为静态变量,如下所示:
private static String latitude;
private static String longitude;
我是Android编码的n00b,今天我想尝试使用定位服务。
我设置了一个简单的class和一个简单的main,只是为了有经度和纬度。
但是当我尝试调用结构来检索经度和纬度时 Android Studio 弹出错误:
"Error:(33, 16) error: non-static variable latitude cannot be referenced from a static context"
这是我的位置Class
public class AppLocationManager implements LocationListener {
private LocationManager locationManager;
private String latitude;
private String longitude;
private Criteria criteria;
private String provider;
public AppLocationManager(Context context) {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,0,this);
setMostRecentLocation(locationManager.getLastKnownLocation(provider));
}
private void setMostRecentLocation(Location lastKnownLocation) {
}
public String getLatitude() {
return latitude;
}
public String getLongitude() {
return longitude;
}
/*
* (non-Javadoc)
*
* @see
* android.location.LocationListener#onLocationChanged(android.location.
* Location)
*/
@Override
public void onLocationChanged(Location location) {
double lon = (double) (location.getLongitude());/// * 1E6);
double lat = (double) (location.getLatitude());// * 1E6);
// int lontitue = (int) lon;
// int latitute = (int) lat;
latitude = lat + "";
longitude = lon + "";
}
/*
* (non-Javadoc)
*
* @see
* android.location.LocationListener#onProviderDisabled(java.lang.String)
*/
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see
* android.location.LocationListener#onProviderEnabled(java.lang.String)
*/
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see android.location.LocationListener#onStatusChanged(java.lang.String,
* int, android.os.Bundle)
*/
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
要访问非静态属性和方法,您需要 class 的对象。要了解阅读 this so thread :
AppLocationManager appLocationManager=new AppLocationManager(context);
String latitude=appLocationManager.getLatitude();
String longitude=appLocationManager.getLongitude();
将您的变量声明为静态变量,如下所示:
private static String latitude;
private static String longitude;