简体   繁体   English

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

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

I have a class GetMyLocation that gets the current location of the user. 我有一个GetMyLocation类,它获取用户的当前位置。 Now I'm trying to call an object of this class from another class, ShowMap2 . 现在,我试图从另一个类ShowMap2调用该类的对象。 But I am unable to send the updated location to the new class. 但是我无法将更新的位置发送给新班级。

I haven't included the other overrides for GetMyLocation here. 我没有在这里包括GetMyLocation的其他替代。

What I have: 我有的:

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

   }

This is the class that gets the location: 这是获取位置的类:

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

The above code returns 0.0 for both lat and lon. 上面的代码为lat和lon返回0.0。

I have tried every possible way I could think of, but I always end up get 0.0. 我尝试了所有可能想到的方法,但最终总是得到0.0。 I know the class itself gets my current lat and lon, but I am not able to pass it correctly. 我知道课程本身会得到当前的经纬度,但我无法正确通过它。 Could some please help me out here? 可以帮我一下吗?

Thanks. 谢谢。

PS: PS:

I found this thread, and tried it accordingly, but to no avail. 我找到了这个线程,并进行了相应的尝试,但无济于事。 Still zeros. 仍然为零。

How to access this variable from an Android onCreate method? 如何从Android onCreate方法访问此变量?

The only difference, in the question in the link, the class is static. 唯一的区别是,在链接中的问题中,类是静态的。 When I make my class static, it says: Modifier 'static' not allowed here. 当我将类设为静态时,它说:此处不允许使用修饰符“静态”。

My updated code: 我更新的代码:

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

    }

EDIT: Updated Code that works: 编辑:更新的代码,可以工作:

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

getting the location values from ShowMap2 using: 使用以下方法从ShowMap2获取位置值:

GetMyLocation myLocation = new GetMyLocation(this);

You're extending Activity in GetMyLocation . 您要在GetMyLocation扩展Activity For the goal you want, remove extends Activity , and move the code from onCreate() to a suitable constructor such as GetMyLocation() . 为了实现您想要的目标,请删除extends Activity ,然后将代码从onCreate()移至合适的构造函数,如GetMyLocation()

The rest of your logic is solid. 您的其余逻辑是可靠的。

An activity is for GUI, and if you don't intend on opening a new activity with your class, don't extend Activity . 活动是针对GUI的,如果您不打算在您的课程中打开新的活动,请不要扩展Activity For the code to work from onCreate() , you'd have to create an intent of your class, and start the activity, which would open a blank activity, because you're not setting any views in GetMyLocation . 为了使代码从onCreate()起作用,您必须创建类的意图,然后启动活动,这将打开一个空白活动,因为您没有在GetMyLocation设置任何视图。

In layman's terms, your onCreate() is never called because you're never starting the activity. 用外行的话来说,永远不会调用onCreate()因为您永远不会开始活动。

EDIT To pass a Context to our class so it runs correctly, we will define our constructor like so: 编辑为了将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();

}

And we will initialize our class like so: GetMyLocation location = new GetMyLocation(this); 然后我们将像这样初始化我们的类: GetMyLocation location = new GetMyLocation(this); from within your Activity . 从您的Activity When we reference this from our class, it points to that class, which just so happens to be an Activity satisfying our GetMyLocation(Context context) constructor. 当我们引用this从我们的类,它指向一个类,这恰好是一个Activity满足我们GetMyLocation(Context context)的构造。 Then you can call your methods and get the expected results. 然后,您可以调用您的方法并获得预期的结果。

When you originally called getSystemService() you implied this which was a reference to your activity that was never started. 当你最初叫getSystemService()你暗示this哪是你的活动的引用从未启动过。 That's also why your IDE got mad at you when you removed extends Activity from GetMyLocation() . 这就是为什么当您从GetMyLocation()删除extends Activity GetMyLocation()时您的IDE会生气的原因。 It didn't know where to look for for getSystemService() , because the implied class this was no longer a Context . 它不知道从哪里找了getSystemService()因为隐含的类this已经不再是一个Context

If you want to pass parameters from Activity to Activity, use Bundle. 如果要将参数从“活动”传递到“活动”,请使用“捆绑销售”。 If you want to get result when Activity is exiting, use startActivityForRestult when creating the new Activity. 如果要在Activity退出时获得结果,请在创建新的Activity时使用startActivityForRestult。 If you just want to pass parameters from Activity to another class, use the class's constructor. 如果仅要将参数从Activity传递到另一个类,请使用该类的构造函数。

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

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