从外部类(非活动)调用方法,以便在打开片段时执行



我试图在这里找到答案。我认为我没有正确解释答案,但以防万一这是我的坐姿。我正在使用主活动中的按钮打开片段。打开片段后,我希望外部类执行数学程序并在片段中显示结果。我可以让结果显示在片段中,但我无法让数学程序执行。我知道如何从外部类调用方法,但我不确定在哪里输入。我会把它放在片段还是主要活动中?

我的第一次尝试是使用类似的东西: 汞测试=新的汞((; 测试.m计算;

这有效,但我不确定如何使用它。

以下是主要活动:

public class MainActivity extends     
AppCompatActivity implements 
TestFragment.OnFragmentInteractionListener {
Button btnFavourites;
Mercury mercury;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnFavourites = (Button) findViewById(R.id.button_test);
btnFavourites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

android.support.v4.app.FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
TestFragment fragment = new TestFragment();
fragmentTransaction.add(R.id.frame_layout, fragment);
fragmentTransaction.commit();
btnFavourites.setVisibility(View.GONE);
}
});

}
@Override
public void onFragmentInteraction(Uri uri) {
}

}

这是片段:

public class TestFragment extends Fragment {
// TODO: Rename parameter arguments, choose    
names that match
// the fragment initialization parameters,    
e.g.     ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private TextView planetLongitude;
private TextView planetLatitude;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
Mercury test;
private OnFragmentInteractionListener mListener;
public TestFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment TestFragment.
*/
// TODO: Rename and change types and number of parameters
public static TestFragment newInstance(String param1, String param2) {
TestFragment fragment = new TestFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);

}


}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_test, container, false);


planetLongitude = (TextView) view.findViewById(R.id.latitude);
planetLongitude.setText("" + Mercury.getLonecl());


return view;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

}

这是我在打开片段时要执行的 outclass:

public class Mercury {
public static double xv = 0;
public static double yv = 0;
private static double lonecl = 0;
private static double latecl = 0;
public static double N = 48.3313 + 3.24587e-5 * PlanetVariableCalculations.getd2();
public static double i = 7.0047 + 5.00e-8 * PlanetVariableCalculations.getd2();
public static double w = 29.1241 + 1.0144e-5 * PlanetVariableCalculations.getd2();
public static double a = 0.387098;
public static double e = 0.205635 + 5.59e-10 * PlanetVariableCalculations.getd2();
public static double M = 168.6562 + 4.0923344368 * PlanetVariableCalculations.getd2();
public static double w1 = N + w;
public static double L = M + w1;
public static double q = a * (1 - e);
public static double Q = a * (1 + e);
public static double E0 = 0;
public static double P = Math.pow(a, 1.5);

private void mCalculation() {
if (M < 0) {
while (M < 0) {
M = M + 360;
}
} else if (M > 360) {
while (M > 360) {
M = M - 360;
}
}
EccentricAnomaly(M);
}
private void EccentricAnomaly(double M) {

double angleInRadian = Math.toRadians(M);
double E = M + e * Math.sin(angleInRadian) * (1.0 + e * Math.cos(angleInRadian));
if (e > 0.06) {
E0 = E;
double angleInRadianE = Math.toRadians(E0);
double E1 = E0 - (E0 - e * Math.sin(angleInRadianE) - M) / (1 - e * Math.cos(angleInRadianE));
while (E1 != E0) {
E1 = E0;
E1 = E0 - (E0 - e * Math.sin(angleInRadianE) - M) / (1 - e * Math.cos(angleInRadianE));
}
xv = Math.cos(E1) - e;
yv = Math.sqrt(1.0 - e * e) * Math.sin(E1);
//} else{
xv = Math.cos(E) - e;
yv = Math.sqrt(1.0 - e * e) * Math.sin(E);
}
double v = Math.atan2(yv, xv);
double r = Math.sqrt(xv * xv + yv * yv);
double xh = r * (Math.cos(N) * Math.cos(v + w) - Math.sin(N) * Math.sin(v + w) * Math.cos(i));
double yh = r * (Math.sin(N) * Math.cos(v + w) - Math.cos(N) * Math.sin(v + w) * Math.cos(i));
double zh = r * (Math.sin(v + w) * Math.sin(i));
double lacteclsqr = Math.sqrt(xh * xh + yh * yh);
lonecl = 2+2;
latecl = Math.atan2(zh,lacteclsqr );

}
public static double getLonecl(){
return lonecl;
}
public static double getLatecl(){
return latecl;
}

}

我会尝试回答这个问题,尽管我不完全确定你所说的"我无法执行数学程序"是什么意思。

我已经可以说,您可能需要阅读更多有关static关键字含义的信息,也许从这里开始。您还需要确定是否确实需要创建实例。

如果您的类不保留信息,只执行计算并返回值,则可能应该将其设置为静态。

double lonEcl = Mercury.computeLonEcl();

我认为您的问题是方法private void mCalculation()私有的,这意味着它只能在类Mercury中访问。您可以通过public替换private并通过如下所示的实例使用它:

Mercury instance = new Mercury();
instance.mCalculation();
instance.getLonEcl();

此外,您应该在类private static final...中将这些常量设为公共,或者在类外需要它们时将它们公开。

这里有几件事。

第一。

您不会打开片段。添加、删除、显示或隐藏片段。

除非您使用的是 DialogFragment,否则这是不同的。

第二。

帮助程序类没有理由保留实例。所以没有理由这样做。在数学类中使您的方法静态并像访问它们一样

Mercury.doMyMathProblem(args...)

现在,如果您说"嘿,我仍然想使用实例"。那么,如果您只需要整个应用程序的一个单例,则可以执行单例,或者您可以根据需要简单地将其更新。

new Mercury().doMyMathProblems(args...)

如果您需要将其存储在片段的实例变量中,则只需在onCreate方法中创建它即可。

onCreate(){
mMercuryInstance = new Mercury()
}
onButtonClick(){
Int answer = mMercuryInstance.doMyMathProblem(args...)
}

当然,这只是伪代码,但应该明白这一点。

最新更新