如何在ViewPager适配器中获取字符串资源? [英] How to get String Resource within ViewPager Adapter?
问题描述
我试图为我的viewpager设置标题,但似乎无法正常使用.我尝试了Resources.getSystem().getString(R.string.title1);并尝试传递上下文.有人可以帮我吗?
I trying to set the title for my viewpager, I can't seem to get it to work. I tried Resources.getSystem().getString(R.string.title1); and also tried to pass a context. Could anyone help me?
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
Context context;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return PageFragment.create(position + 1);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return Resources.getSystem().getString(R.string.title1);
case 1:
return Resources.getSystem().getString(R.string.title1);
case 2:
return Resources.getSystem().getString(R.string.title1);
}
return null;
}
}
Logcat:
09-02 17:40:33.160: E/AndroidRuntime(3116): FATAL EXCEPTION: main
09-02 17:40:33.160: E/AndroidRuntime(3116): android.content.res.Resources$NotFoundException: String resource ID #0x7f050003
09-02 17:40:33.160: E/AndroidRuntime(3116): at android.content.res.Resources.getText(Resources.java:230)
09-02 17:40:33.160: E/AndroidRuntime(3116): at android.content.res.Resources.getString(Resources.java:314)
推荐答案
好吧,根据您的错误消息,您没有要求的ID的字符串.
Well, according to your error message you do not have a String with the ID you are requesting.
可能是您支持多种语言,并且仅具有此功能 一种特定语言的String-ID?
Could it be that you are supporting multi languages and only have this kind of String-ID for a specific language?
无论如何,这行代码:
String yourstring = getResources().getString(R.string.yourstring);
是如何从资源中获取字符串.只要您在扩展Context
的类(例如Activity
)中,都可以调用getResources()
.
is how to get a String from Resources. getResources()
can be called whenever you are in a class extending Context
, such as Activity
.
这篇关于如何在ViewPager适配器中获取字符串资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!