简体   繁体   English

扩展Fragment的类的调用方法

[英]Call method from class which extends Fragment

I have StartActivity class. 我有StartActivity类。 It extends from Fragment : 它从Fragment扩展:

The code for StarActivity.java is shown below: StarActivity.java的代码如下所示:

public class StartActivity extends Fragment {
    public static Context appContext;
    ActionBar actionbar;
    int state = 0;
    /** Called when the activity is first created. */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main, container,
                false);
        return rootView;
    }

    // call draw Tab method
    public void drawTab(){
          //ActionBar
        actionbar = getActivity().getActionBar();
        if(state == 0){
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
        ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");

        Fragment PlayerFragment = new AFragment();
        Fragment StationsFragment = new BFragment();

        PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
        StationsTab.setTabListener(new MyTabsListener(StationsFragment));

        actionbar.addTab(PlayerTab);
        actionbar.addTab(StationsTab);
        state++;
        }
    }

In MainClass.java I declare a variable: Fragment fragment1 = new StartActivity(); 在MainClass.java中,我声明一个变量: Fragment fragment1 = new StartActivity(); How to I can call drawTab method. 如何调用drawTab方法。 I try fragment.drawTab but it can't. 我尝试fragment.drawTab但不能。 If I call drawTab in onCreateView in StartActivity.java, it runs OK. 如果我在StartActivity.java, onCreateView中调用drawTab StartActivity.java,它将运行正常。 but when I don't call drawTab() in onCreateView, error happens:( RUN ERROR: 但是当我不在onCreateView中调用drawTab()时,发生错误:( RUN ERROR:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main, container,
                false);
        return rootView;
    }

RUN OK: 运行确定:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.main, container,
                    false);
            drawTab();
            return rootView;
        }

MainClass.java MainClass.java

private void selectItem(int position) {
        Toast.makeText(getBaseContext(), Integer.toString(position), 1).show();
        Fragment fragment1 = new StartActivity();
        switch (position) {
        case 5:
            break;
        case 1:
            ((StartActivity) fragment1).drawTab();
            FragmentManager fragmentManager1 = getFragmentManager();
            fragmentManager1.beginTransaction().replace(R.id.content_frame, fragment1).commit();
            break;
        default:
            break;
        }

The problem is when you create an instace of a fragment, it is not attached to the activity yet. 问题是当您创建片段的实例时,它尚未附加到活动中。 So drawing a tab before onAttach method will cause a NPE error. 因此,在onAttach方法之前绘制选项卡将导致NPE错误。

Try to do something below: 尝试执行以下操作:

public class StartActivity extends Fragment {
    // TODO: add your other/current fields here
    boolean mDrawTabWhileInitializing = false;
    public StartActivity(boolean drawTabWhileInitializing) {
        mDrawTabWhileInitializing = drawTabWhileInitializing;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main, container, false);
        if(mDrawTabWhileInitializing) {
            drawTab();
        }
        return rootView;
    }    

    // TODO: add your other/current methods here
}

Use the boolean flag while initializing your fragment: 在初始化片段时使用boolean标志:

private void selectItem(int position) {
    Toast.makeText(getBaseContext(), Integer.toString(position), 1).show();
    Fragment fragment1 = null;
    switch (position) {
        case 1:
            fragment1 = new StartActivity(true);
            FragmentManager fragmentManager1 = getFragmentManager();
            fragmentManager1.beginTransaction().replace(R.id.content_frame, fragment1).commit();
            break;
        default:
            break;
    }
}

Note: Just an advice, the name of a fragment is annoying. 注意:只是一个建议,片段的名称很烦人。 Why don't you name it StartFragment? 为什么不将其命名为StartFragment?

将其声明为StartActivity Fragment1 = new StartActivity();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用类从扩展Fragment的类扩展Fragment方法 - Call class extends Fragment method from a class extends Fragment 调用从MainActivity类扩展Activity的Fragment扩展的类Fragment - Calling a Class Fragment which extends Fragment from a MainActivity Class which extends Activity 从扩展Activity的类中调用View - Call a View from a class which extends Activity 如何从扩展 JobService 的类调用 MainActivity 的方法,该方法将视图作为参数? - How to call a method from MainActivity which takes views as parameters, from a class which extends JobService? 如何在扩展 Swingworker 的类的 done() 方法中调用类? - How to call a class in the done() method of a class which extends Swingworker? 从另一个类调用一个扩展Thread的类的方法 - Calling a method of a class which extends Thread, from another class 调用从主方法扩展另一个类的类 - Call a class that extends another class from main method Java:如何在主类中调用方法,而该方法在另一个类中,该类扩展了抽象类 - Java: How to call upon a method in a main class where the method is in another class which extends an abstract class 从扩展应用程序的另一个 class 的活动中调用方法 - Call method in an activity from another class that extends Application 从无关类中调用片段中的方法 - Call Method in Fragment from Unrelated Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM