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

时间:2015-02-27 05:16:32

标签: java android android-activity oncreate

我有一个 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。我知道类本身会得到我当前的lat和lon,但我无法正确传递它。有人可以帮帮我吗?

感谢。

PS:

我找到了这个帖子,并据此尝试了,但无济于事。还是零。

How to access this variable from an Android onCreate method?

唯一的区别是,在链接的问题中,类是静态的。当我让我的课静态时,它说:修饰符'静态'不允许在这里。

我的更新代码:

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

2 个答案:

答案 0 :(得分:0)

您在Activity中展开GetMyLocation。对于您想要的目标,请移除extends Activity,然后将代码从onCreate()移至合适的构造函数,例如GetMyLocation()

你的其余逻辑是坚实的。

活动适用于GUI,如果您不打算在课堂上开设新活动,请不要延长Activity。要使代码从onCreate()开始工作,您必须创建类的意图,然后启动活动,这会打开一个空白活动,因为您未在{中设置任何视图{1}}。

通俗地说,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移除extends Activity时,IDE对您生气的原因。它不知道在哪里查找GetMyLocation(),因为隐含的类getSystemService()不再是this

答案 1 :(得分:-1)

如果要将参数从Activity传递给Activity,请使用Bundle。 如果要在Activity退出时获取结果,请在创建新Activity时使用startActivityForRestult。 如果您只想将参数从Activity传递到另一个类,请使用类的构造函数。