首先,我尝试了几乎所有关于 StackOverflow 的答案来试试这个问题。!!但没有任何作用。我正在分享我的节目。请看一下,让我知道如何解决这个问题。
以前我使用的是 getFragmentManger()方法,但它会抛出相同的异常。然后我尝试了这个 getChildFragmentManager()方法。但它仍然显示了同样的例外.. !!
这是代码:
public class FragmentC extends Fragment {
private final List<String> mListGroup = new ArrayList<String>();
private ViewPager viewPager;
private ListPagerAdapter groupsPagesAdapter;
private PagerTabStrip pagerTabs;
public FragmentC() {
// Required empty public constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewPager = (ViewPager) ((Activity) parentContext).findViewById(R.id.pager);
groupsPagesAdapter = new ListPagerAdapter(getChildFragmentManager(), viewPager); // Here I made the change, But still fails
pagerTabs = (PagerTabStrip) ((Activity) parentContext).findViewById(R.id.tabstrip);
pagerTabs.setTabIndicatorColorResource(R.color.vert_manu);
pagerTabs.setNonPrimaryAlpha(PAGER_TAB_SECONDARY_ALPHA);
mListGroup.add(getString(R.string.contact_list_all_contact));
mListGroup.add(getString(R.string.contact_list_no_group));
groupsPagesAdapter.notifyDataSetChanged();
View v= inflater.inflate(R.layout.contactlist, container, false);
return v;
}
private class ListPagerAdapter extends FragmentPagerAdapter {
public ListPagerAdapter(final FragmentManager fm, final ViewPager viewPager) {
super(fm);
viewPager.setAdapter(this);
}
@Override
public Fragment getItem(int position) {
String group = mListGroup.get(position);
ContactListFragment f = ContactListFragment.newInstance(group);
f.setListAdapter(getContactListAdapter(group));
return f;
}
@Override
public int getCount() {
return mListGroup.size();
}
@Override
public String getPageTitle(int position) {
return mListGroup.get(position);
}
}
}