不能使用方法"getString(int resId)";在FragmentPagerAdapter中 [英] can't use method "getString(int resId)" in FragmentPagerAdapter

查看:191
本文介绍了不能使用方法"getString(int resId)";在FragmentPagerAdapter中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我现在在片段活动中具有以下类,现在我想从strings.xml获取选项卡的名称,但是我无法使用方法getString(int resId),它给出了一条错误消息无法静态引用类型Context中的非静态方法getString(int).
  2. 在方法public CharSequence getPageTitle(int position)中,我需要获得这样的name = getString(R.string.mystring)任何帮助.
  1. I've the following class in my fragment activity now i want to get name of tabs from strings.xml but i can't able to use method getString(int resId) it gives a error message "Cannot make a static reference to the non-static method getString(int) from the type Context".
  2. In method public CharSequence getPageTitle(int position) I need to get like this name = getString(R.string.mystring) any help is appreciated.

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

          public AppSectionsPagerAdapter(FragmentManager fm) {
              super(fm);
          }

          @Override
          public Fragment getItem(int i) {
              switch (i) {
              case 0:
                  Fragment fragment = new Activity1();
                  return fragment;

              case 1:
                  Fragment fragment1 = new Activity2();
                  return fragment1;

              default:
                  return new Activity2();
              }
          }

          @Override
          public int getCount() {
              return 2;
          }

          public CharSequence getPageTitle(int position) {
              String name = null;
              if (position==0) {
                  name = "Movie Details";
              }else if (position==1) {
                  name = "Movie Comments";
              }
              return name;
          }
      }

推荐答案

您需要Context对象才能调用getResources().getString(int resID).尝试在构造函数中传递Context,并在此处像

You need a Context object to call getResources().getString(int resID). Try passing a Context in your constructor and using it here like

Context mContext;

public AppSectionsPagerAdapter(FragmentManager fm,Context context) {
          super(fm);
           mContext = context
      }

,然后使用mContext.getResources().getString(int resID)

这篇关于不能使用方法"getString(int resId)";在FragmentPagerAdapter中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆