Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have StartActivity class. It extends from Fragment:

The code for StarActivity.java is shown below:

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(); How to I can call drawTab method. I try fragment.drawTab but it can't. If I call drawTab in onCreateView in StartActivity.java, it runs OK. but when I don't call drawTab() in onCreateView, error happens:( 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

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;
        }
share|improve this question

2 Answers 2

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.

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:

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?

share|improve this answer
    
If I call drawTab in onCreateView in StartActivity.java, it runs OK. but when I don't call drawTab() in onCreateView, error happens –  Van Der Cong Feb 24 at 8:23
    
what error? share log cat. –  aegean Feb 24 at 8:31
    
No notification. Only ": E/(): Device disconnected: 1" –  Van Der Cong Feb 24 at 8:35
    
Have you tried putting ((StartActivity) fragment1).drawTab(); after replace (in your swithc-case statement)? –  aegean Feb 24 at 8:37
    
I tried but it doesn't work. I used StartActivity fragment1 = new StartActivity(); fragment1.drawTab(); but it also doesn't work. I don't know reason –  Van Der Cong Feb 24 at 8:42

Declare it as StartActivity Fragment1 = new StartActivity();

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.