首页 文章

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

提问于
浏览 1055
0

我的观点是,我能够在片段的onCreate中获得经度和经度 . 我想要做的是然后将这些值发送到我的Sun公共类,但我的尝试表明这些值在Sun Public类中为零 . 然而,我的logcat显示我的片段中正在生成这些值 . 这告诉我我的位置代码确实有效,我只是把这些值放到我的公共类中 .

因此,我的问题是,是否有办法从片段中将这些值发送到公共类,或者是否可能在onCreate中生成经度和纬度之前调用在onCreateView中调用的实例?如果是这种情况,有一种方法可以在生成onCreateView之前获取经度和纬度值 . 基本上我需要从片段中的位置在我的公共sun类中进行计算 . 位置以粗体显示 . 从我的理解onCreate是在onCreateView之前生成的我不知道为什么会出现这种情况 . 这是我的代码:

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

}

}

1 回答

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

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

    Sun sun = new Sun();
    

    并调用它的 SunmCalculation() 方法如下:

    sun.SunmCalculation(longitude);
    

相关问题