0

我正在尝试从我的 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();
4

0 回答 0