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);
是如何从资源中获取字符串.getResources()
可以在您处于扩展 Context
的类中时调用,例如 Activity
.
is how to get a String from Resources. getResources()
can be called whenever you are in a class extending Context
, such as Activity
.
这篇关于如何在 ViewPager 适配器中获取字符串资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 ViewPager 适配器中获取字符串资源?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01