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

本文标题为:如何在 ViewPager 适配器中获取字符串资源?

基础教程推荐