繁体   English   中英

Android-从另一个类的oncreate()获取变量

[英]Android - Get variable from oncreate() of another class

我有一个GetMyLocation类,它获取用户的当前位置。 现在,我试图从另一个类ShowMap2调用该类的对象。 但是我无法将更新的位置发送给新班级。

我没有在这里包括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();

   }

这是获取位置的类:

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;
    }

上面的代码为lat和lon返回0.0。

我尝试了所有可能想到的方法,但最终总是得到0.0。 我知道课程本身会得到当前的经纬度,但我无法正确通过它。 可以帮我一下吗?

谢谢。

PS:

我找到了这个线程,并进行了相应的尝试,但无济于事。 仍然为零。

如何从Android onCreate方法访问此变量?

唯一的区别是,在链接中的问题中,类是静态的。 当我将类设为静态时,它说:此处不允许使用修饰符“静态”。

我更新的代码:

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()

您的其余逻辑是可靠的。

活动是针对GUI的,如果您不打算在您的课程中打开新的活动,请不要扩展Activity 为了使代码从onCreate()起作用,您必须创建类的意图,然后启动活动,这将打开一个空白活动,因为您没有在GetMyLocation设置任何视图。

用外行的话来说,永远不会调用onCreate()因为您永远不会开始活动。

编辑为了将Context传递给我们的类以便它能够正确运行,我们将像下面这样定义构造函数:

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();

}

然后我们将像这样初始化我们的类: GetMyLocation location = new GetMyLocation(this); 从您的Activity 当我们引用this从我们的类,它指向一个类,这恰好是一个Activity满足我们GetMyLocation(Context context)的构造。 然后,您可以调用您的方法并获得预期的结果。

当你最初叫getSystemService()你暗示this哪是你的活动的引用从未启动过。 这就是为什么当您从GetMyLocation()删除extends Activity GetMyLocation()时您的IDE会生气的原因。 它不知道从哪里找了getSystemService()因为隐含的类this已经不再是一个Context

如果要将参数从“活动”传递到“活动”,请使用“捆绑销售”。 如果要在Activity退出时获得结果,请在创建新的Activity时使用startActivityForRestult。 如果仅要将参数从Activity传递到另一个类,请使用该类的构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM