我正在尝试在fragment中创建可滑动的标签。在viewpager中添加标签。但它给了我错误 当actionBar.setHomeButtonEnabled(false)为空指针异常无法进行instastaite活动时。我尝试解决过去两天的问题但无法解决问题。请帮我解决它。请找我的Fragment类。
public class HomeFragment扩展了Fragment工具 ActionBar.TabListener {
View FaramentView;
private FragmentActivity context;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
FaramentView = inflater.inflate(R.layout.home_fragment, null, false);
return FaramentView;
}
@Override public void onAttach(Activity activity) {
context = (FragmentActivity) activity;
super.onAttach(activity);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
// Initilization
viewPager = (ViewPager) FaramentView.findViewById(R.id.pager);
actionBar = context.getActionBar();
mAdapter = new TabsPagerAdapter(context.getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
public LayoutInflater getSystemService(String layoutInflaterService) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:7)
在活动添加
下的Android清单文件中android:theme="@android:style/Theme.Holo"
我想将此添加为评论,但由于声誉的数量,我无法做到。