gpt4 book ai didi

android - fragment 中未定义的 getSupportFragmentManager()

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:54 30 4
gpt4 key购买 nike

我在 getSupportFragmentManager() 上遇到错误“此方法在 Fragment 中未定义”,同时创建带有滑动选项卡的 slider 菜单我准备了如下代码..

public class HomeFragment extends Fragment {

Toolbar toolbar;
ViewPager viewpager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles1[] = { "Home", "Help", "Contect" };
// int Titles[] = { R.drawable.ic_content_copy_black_24dp,
// R.drawable.ic_refresh_black_24dp, R.drawable.ic_share_black_24dp };
int NumOfTabs = 3;

public HomeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_home, container,
false);

return rootView;

adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles1,
NumOfTabs);

viewpager = (ViewPager) rootView.findViewById(R.id.pager);
viewpager.setAdapter(adapter);

tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
}

我的 ViewPager 适配器类如下所示

public class ViewPagerAdapter extends FragmentStatePagerAdapter{

CharSequence Titles1[];
int Titles[];
int NumbOfTabs;

public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabs) {
super(fm);
this.Titles1 = mTitles;
this.NumbOfTabs = mNumbOfTabs;
}

@Override
public Fragment getItem(int arg0) {

if(arg0 == 0){
Tab1 tab1 = new Tab1();
return tab1;
}else if(arg0 == 1){
Tab2 tab2 = new Tab2();
return tab2;
}else{
Tab3 tab3 = new Tab3();
return tab3;
}
}


@Override
public CharSequence getPageTitle(int position) {
return Titles1[position];
}

@Override
public int getCount() {
return NumbOfTabs;
}

最佳答案

只需替换:

getSupportFragmentManager()

与:

getFragmentManager()

如果您使用的是 android.support.v4.app.Fragment,它将返回一个 android.support.v4.app.FragmentManager (SupportFragmentManager)

如果您使用的是 android.app.Fragment,它将返回一个 android.app.FragmentManager


请记住,它在 Activities.Like 中的处理方式与 android.support.v4.app.FragmentActivity 中的处理方式不同。你有 getSupportFragmentManager()getFragmentManager()

关于android - fragment 中未定义的 getSupportFragmentManager(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31612506/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com