- 我的 fragment Activity 中有以下类,现在我想从 strings.xml 中获取选项卡的名称,但我无法使用方法
getString(int resId)
它会出错消息“无法从类型 Context 对非静态方法 getString(int) 进行静态引用”。 - 在方法
public CharSequence getPageTitle(int position)
中,我需要像这样name = getString(R.string.mystring)
任何帮助表示赞赏。
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter { public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: Fragment fragment = new Activity1(); return fragment; case 1: Fragment fragment1 = new Activity2(); return fragment1; default: return new Activity2(); } } @Override public int getCount() { return 2; } public CharSequence getPageTitle(int position) { String name = null; if (position==0) { name = "Movie Details"; }else if (position==1) { name = "Movie Comments"; } return name; } }
最佳答案
您需要一个 Context
对象来调用 getResources().getString(int resID)
。尝试在构造函数中传递 Context
并在此处使用它,例如
Context mContext;
public AppSectionsPagerAdapter(FragmentManager fm,Context context) {
super(fm);
mContext = context
}
然后使用mContext.getResources().getString(int resID)
关于java - 无法在 FragmentPagerAdapter 中使用方法 "getString(int resId)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20006736/