有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从非碎片类调用Fragment的方法

我正在尝试从我的未更新类调用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");
}

我曾尝试在我未更新的类中使用此代码,但没有成功(尽管我不知道为什么):

    if (MainActivity.getCurrentFragment() instanceof FragmentActivity) {
        FragmentActivity fragment = (FragmentActivity) MainActivity.getCurrentFragment();
        fragment.methodToBeCalled();

共 (0) 个答案