[英]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.