从扩展Fragment的类调用方法

时间:2014-02-24 08:04:00

标签: java android methods android-fragments

我有StartActivity课程。它从Fragment延伸:

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++;
        }
    }

在MainClass.java中我声明了一个变量:Fragment fragment1 = new StartActivity(); 如何调用drawTab方法。我尝试fragment.drawTab但它不能。如果我在drawTab的{​​{1}}中拨打onCreateView,则表示运行正常。但是当我不在onCreateView中调用StartActivity.java,时,会发生错误:( 运行错误:

drawTab()

RUN OK:

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

MainClass.java

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

2 个答案:

答案 0 :(得分:1)

问题是,当您创建片段的实例时,它尚未附加到活动。因此,在onAttach方法之前绘制标签会导致NPE错误。

尝试做以下事情:

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
}

初始化片段时使用布尔标志:

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;
    }
}

注意:只是一个建议,片段的名称很烦人。你为什么不把它命名为StartFragment?

答案 1 :(得分:0)

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