我试图从我的UnrelatedClass中调用methodToBeCalled()。我是碎片的新手,5天后,仍然无法弄明白。我非常感谢任何详细的帮助!如果我没有包含必要的代码,请告诉我,我会很乐意发布它!谢谢!
public class MainActivity extends FragmentActivity{
Fragment fragment = new FragmentActivity();
}
public class FragmentActivity extends Fragment{
methodToBeCalled();
}
public class UnrelatedClass{
//call FragmentActivity's methodToBeCalled() from here
//note it is not static
}
方法methodToBeCalled()
包含以下代码:
public void methodToBeCalled{
fragmentActivityTextView.setText("Something Else");
}
我已尝试在我的UnrelatedClass中使用此代码,但未成功(虽然我不确定原因):
if (MainActivity.getCurrentFragment() instanceof FragmentActivity) {
FragmentActivity fragment = (FragmentActivity) MainActivity.getCurrentFragment();
fragment.methodToBeCalled();