0

上獲取nullpointerexception我正在將選項卡添加到片段中的操作欄。但是,當我試圖啓動該片段,拋出一個空指針異常在getActionBar()

這之後我的應用程序崩潰是錯誤

Log

的Home.java文件是這樣的:

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) { 

} 
} 

線路號碼40是actionbar = fragmentActivity.getActionBar();

有誰能告訴我爲什麼我面臨這個問題?而不是

回答

5

actionBar = fragmentActivity.getActionBar(); 

使用:

actionBar = getActivity().getActionBar(); 

初始化該活動的方式

fragmentActivity = new FragmentActivity(); 

不會提供一個有效的窗口裝飾擺脫Activity操作欄。

+0

感謝更改onCreateView()方法。這工作。但還有一個問題。 現在,我得到了actionbar.setHomeButtonEnabled(false)中的空指針異常。爲什麼會發生? –

+0

嘗試使用'getSupportActionBar()'...而不是'getActionBar()' – rafsanahmad007

0

如下

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

}

相關問題