0

I am adding tabs to the action bar in a fragment. But when I attempt to launch that fragment, my app crashes after throwing a null pointer exception

This is the error

Log

The Home.java file is this:

public class Home extends Fragment implements ActionBar.TabListener {

private ViewPager viewPager;
private TabPagerAdapter tabPagerAdapter;
private ActionBar actionBar;
private FragmentActivity fragmentActivity;

private String[] tabs ;

public Home() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_home,container,false);
    fragmentActivity = new FragmentActivity();

    tabs = getResources().getStringArray(R.array.hometabs);
    viewPager = (ViewPager)rootView.findViewById(R.id.pager);
    actionBar = fragmentActivity.getActionBar();
    tabPagerAdapter = new TabPagerAdapter(fragmentActivity.getSupportFragmentManager());

    viewPager.setAdapter(tabPagerAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for(String tab_name: tabs){
        actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
    }

    return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("Home");
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

}
}

Line no. 40 is actionbar = fragmentActivity.getActionBar();

Can anyone tell me why I am facing this issue?

Mayank Aggarwal
  • 139
  • 1
  • 4
  • 14

2 Answers2

5

instead of:

actionBar = fragmentActivity.getActionBar();

use:

actionBar = getActivity().getActionBar();

the way you initialize the activity

 fragmentActivity = new FragmentActivity();

will not provide a valid window decor to get the Action bar from Activity.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

change your onCreateView() method as follows

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    // Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home,container,false);
fragmentActivity = getActivity();

tabs = getResources().getStringArray(R.array.hometabs);
viewPager = (ViewPager)rootView.findViewById(R.id.pager);
actionBar = fragmentActivity.getActionBar();
tabPagerAdapter = new TabPagerAdapter(fragmentActivity.getSupportFragmentManager());

viewPager.setAdapter(tabPagerAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

for(String tab_name: tabs){
    actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}

return rootView;

}

Dehan Wjiesekara
  • 3,152
  • 3
  • 32
  • 46