简体   繁体   English

如何将对象(在这种情况下为double)从片段传递到公共类?

[英]How do pass an object(double in this case) from fragment to public class?

My situtation is that I am able to get the latitude and longitude in the onCreate of my Fragment. 我的假设是,我能够在片段的onCreate中获得经度和纬度。 What I want to do is then send those values to my Sun public class but my attempts show that the those values are zero in the Sun Public class. 然后,我想将这些值发送到Sun Public类中,但是我的尝试表明这些值在Sun Public类中为零。 My logcat shows however that those values are being generated in my fragment. 但是,我的logcat显示我的片段中正在生成这些值。 This tells me that my location code does work , I just get those values into my public class. 这告诉我,我的位置代码确实有效,我只是将这些值添加到我的公共类中。

Therefore, my question is whether there a way to send those values to a public class from a fragment or is it possible that the instance being called in my onCreateView is being called before the longitude and latitude are generated in onCreate? 因此,我的问题是是否有一种方法可以将这些值从片段发送到公共类,或者是否有可能在onCreate中生成经度和纬度之前调用了onCreateView中正在调用的实例? If that is the case is there a way to get the longitude and latitude values first before onCreateView is generated. 如果是这样,则可以在生成onCreateView之前先获取经度和纬度值。 Basically I need the location from the fragment to put in a calculation in my public sun class.The location in bold. 基本上,我需要将片段的位置放入我的公共阳光课中的计算中,该位置以粗体显示。 From my understanding onCreate is generated before onCreateView I'm not sure why this would be the case. 根据我的理解,onCreate是在onCreateView之前生成的,我不确定为什么会这样。 Here is my code: 这是我的代码:

Fragment 分段

    public class TestFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);


    requestPermission();

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());

    if (ActivityCompat.checkSelfPermission(getActivity(), ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)  {

        return;
    }
    mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {

            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();

                System.out.println("This is latitude:" + latitude);
                System.out.println("This is longitude:" + longitude);



            }

        }
    });


    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);



    }




}

private void requestPermission(){
    ActivityCompat.requestPermissions(getActivity(),new String[]{ACCESS_FINE_LOCATION}, 1);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    Sun sunInstance = new Sun();
    sunInstance.SunmCalculation();

 return view; 

Public Class Sun 太阳公共课

    public class Sun {
    private double M = 356.0470 + 0.9856002585 * PlanetVariableCalculations.getd2();
    private double w =  282.9404 +  4.70935e-5 * PlanetVariableCalculations.getd2();

  public void SunmCalculation() {
    if (M < 0) {
        while (M < 0) {
            M = M + 360;
        }

    } else if (M > 360) {
        while (M > 360) {
            M = M - 360;
        }
    }

    double L = w + M;
    SunSidereal(L);

}

private void SunSidereal(Double L){
     double Ls = L;
    double RAhours = 300;
    double GMST0 = Ls/15 +12;
    double SIDTIME = GMST0 + UTCValues.getutcComplete() + **Longitude**/15;
    double HA = SIDTIME - RAhours;

    System.out.println("This is SIDTIME:" + SIDTIME);
    System.out.println("This is HA:" + HA);
    System.out.println("This is the longitude: " + Longitude  );

}

} }

Change the method signatures in the class like this: 像这样更改类中的方法签名:

public class Sun {
    private double M = 356.0470 + 0.9856002585 * PlanetVariableCalculations.getd2();
    private double w =  282.9404 +  4.70935e-5 * PlanetVariableCalculations.getd2();

    public void SunmCalculation(Double longtitude) {
        if (M < 0) {
            while (M < 0) {
                M = M + 360;
            }

        } else if (M > 360) {
            while (M > 360) {
                M = M - 360;
            }
        }

        double L = w + M;
        SunSidereal(L, longtitude);
    }

    private void SunSidereal(Double L, Double longtitude){
        double Ls = L;
        double RAhours = 300;
        double GMST0 = Ls/15 +12;
        double SIDTIME = GMST0 + UTCValues.getutcComplete() + longitude / 15;
        double HA = SIDTIME - RAhours;

        System.out.println("This is SIDTIME:" + SIDTIME);
        System.out.println("This is HA:" + HA);
        System.out.println("This is the longitude: " + Longitude  );
    }
}

You can create an object of the class Sun inside onSuccess() : 您可以在onSuccess()创建Sun类的对象:

Sun sun = new Sun();

and call its SunmCalculation() method like this: 并这样调用其SunmCalculation()方法:

sun.SunmCalculation(longitude);

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

相关问题 如何将数据从类传递到片段? - How do I pass data from a class to a fragment? 如何将值从公共类传递到另一个公共类? - How to pass values from public class to another public class? 如何将数据从Java类传递到片段 - How to pass data from java class to fragment 如何将片段 class 中的数据传递给活动 - How to pass data from a fragment class to an activity 如何将另一个类中的对象传递给paintComponent方法? - How do I pass an object from another class into the paintComponent method? 如何在Fragment类中从公共静态void调用到非静态公共void - How can call from public static void to not static public void in Fragment class 如何将自定义对象传递到其他片段中的列表? - How do I pass a Custom Object to a list in a different fragment? 无法从片段对象访问公共方法 - can not access the public methods from fragment object 如何通过类A将Java中的对象从类B传递到类C而不重复 - How do I pass an object in Java from Class B to Class C via Class A without duplication 如何将 object 从一个 class 分配给另一个 class 以及如何在 Z57A97A39435CFDFED96E03 中计算 double 和 int? - How do I assign object from one class to another class and how do I calculate double and int inside an ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM