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

时间:2018-12-08 14:08:52

标签: java android android-fragments

我的假设是,我能够在我的Fragment的onCreate中获得纬度和经度。然后,我想将这些值发送到Sun Public类中,但是我的尝试表明这些值在Sun Public类中为零。但是,我的logcat显示我的片段中正在生成这些值。这告诉我,我的位置代码确实有效,我只是将这些值添加到我的公共类中。

因此,我的问题是是否有办法将这些值从片段发送到公共类,或者是否有可能在onCreate中生成经度和纬度之前调用了onCreateView中正在调用的实例?如果是这样,则可以在生成onCreateView之前先获取经度和纬度值。基本上,我需要将片段的位置放入我的公共阳光课中的计算中,该位置以粗体显示。根据我的理解,onCreate是在onCreateView之前生成的,我不确定为什么会这样。这是我的代码:

片段

    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; 

公共课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  );

}

}

1 个答案:

答案 0 :(得分:0)

像这样更改类中的方法签名:

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

您可以在Sun内创建类onSuccess()的对象:

Sun sun = new Sun();

并按如下所示调用其SunmCalculation()方法:

sun.SunmCalculation(longitude);