Android - 从另一个 class 的 oncreate() 获取变量
Android - Get variable from oncreate() of another class
我有一个 class GetMyLocation 可以获取用户的当前位置。现在我试图从另一个 class、ShowMap2 调用这个 class 的对象。但是我无法将更新后的位置发送到新的 class.
我没有在此处包括 GetMyLocation 的其他覆盖。
我有:
public class ShowMap2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showmap);
GetMyLocation myLocation = new GetMyLocation();
myLat = myLocation.GetCurrentLat();
myLon = myLocation.GetCurrentLon();
}
这是获取位置的class:
public class GetMyLocation extends Activity implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
Location location;
public static double myLat, myLon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
this.myLat = location.getLatitude();
this.myLon = location.getLongitude();
}
public double GetCurrentLat(){
return this.myLat;
}
public double GetCurrentLon(){
return this.myLon;
}
上面的代码returns纬度和经度都为0.0。
我想过所有可能的方法,但我总是得到 0.0。我知道 class 本身获取我当前的纬度和经度,但我无法正确传递它。有人可以帮我解决这个问题吗?
谢谢。
PS:
我找到了这个帖子,并相应地进行了尝试,但无济于事。仍然为零。
How to access this variable from an Android onCreate method?
唯一的区别,在link的问题中,class是静态的。当我将 class 设置为静态时,它会显示:此处不允许使用修饰符 'static'。
我更新的代码:
public class GetMyLocation extends Activity implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
Location location;
static double myLat, myLon;
double GetCurrentLat(){
return myLat;
}
double GetCurrentLon(){
return myLon;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
this.myLat = location.getLongitude();
this.myLon = location.getLongitude();
}
编辑:更新的有效代码:
public class GetMyLocation implements LocationListener {
//public class GetMyLocation extends Activity implements LocationListener {
private static final LatLng HAMBURG = new LatLng(43.48484521,-80.5274279);
private GoogleMap map;
protected LocationManager locationManager;
protected LocationListener locationListener;
Location location;
private static double myLat, myLon;
GetMyLocation(Activity activity){
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
this.myLat = location.getLatitude();
this.myLon = location.getLongitude();
}
double GetCurrentLat(){
return this.myLat;
}
double GetCurrentLon(){
return this.myLon;
}
使用以下方法从 ShowMap2 获取位置值:
GetMyLocation myLocation = new GetMyLocation(this);
您正在 GetMyLocation
中扩展 Activity
。为了您想要的目标,删除 extends Activity
,并将代码从 onCreate()
移动到合适的构造函数,例如 GetMyLocation()
.
你其余的逻辑是可靠的。
activity 用于 GUI,如果您不打算用 class 打开一个新的 activity,请不要扩展 Activity
。要使代码从 onCreate()
开始工作,您必须创建 class 的意图,然后启动 activity,这将打开一个空白 activity,因为您在 GetMyLocation
.
通俗地说,您的 onCreate()
从未被调用,因为您从未启动 activity。
编辑 要将 Context
传递给我们的 class 以便它正确运行,我们将像这样定义我们的构造函数:
GetMyLocation(Context context) {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
this.myLat = location.getLatitude();
this.myLon = location.getLongitude();
}
我们将像这样初始化我们的 class:从您的 Activity
中 GetMyLocation location = new GetMyLocation(this);
。当我们从 class 引用 this
时,它指向那个 class,恰好是一个 Activity
满足我们的 GetMyLocation(Context context)
构造函数。然后您可以调用您的方法并获得预期的结果。
当您最初调用 getSystemService()
时,您暗示 this
这是对您从未启动过的 activity 的引用。这也是为什么当你从 GetMyLocation()
中删除 extends Activity
时你的 IDE 生你的气的原因。它不知道去哪里寻找 getSystemService()
,因为隐含的 class this
不再是 Context
.
如果要将参数从 Activity 传递到 Activity,请使用 Bundle。 如果您想在 Activity 退出时获得结果,请在创建新的 Activity 时使用 startActivityForRestult。 如果您只想将参数从 Activity 传递到另一个 class,请使用 class 的构造函数。