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 适配器中获取字符串资源?
基础教程推荐
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
