0

J'ajoute des onglets à la barre d'action dans un fragment. Mais lorsque je tente de lancer ce fragment, mon application se bloque après avoir jeté une exception de pointeur nulObtenir nullpointerexception sur getActionBar()

C'est l'erreur

Log

Le fichier Home.java est la suivante:

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 est actionbar = fragmentActivity.getActionBar();

Quelqu'un peut-il me dire pourquoi je suis confronté à ce problème?

Répondre

5

au lieu de:

actionBar = fragmentActivity.getActionBar(); 

utilisation:

actionBar = getActivity().getActionBar(); 

la façon dont vous initialisez l'activité

fragmentActivity = new FragmentActivity(); 

ne fournira pas un décor de fenêtre valide pour obtenir la barre d'action de Activity .

+0

Merci. Cela a fonctionné. Mais juste une question de plus. Maintenant, j'obtiens une exception de pointeur null dans actionbar.setHomeButtonEnabled (false). Pourquoi cela se passe-t-il? –

+0

essayez d'utiliser 'getSupportActionBar()' ... au lieu de 'getActionBar()' – rafsanahmad007

0

changer votre méthode onCreateView() comme suit

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

}