如何在 ViewPager 适配器中获取字符串资源?

How to get String Resource within ViewPager Adapter?(如何在 ViewPager 适配器中获取字符串资源?)
本文介绍了如何在 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 适配器中获取字符串资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)