【问题标题】:can't use method "getString(int resId)" in FragmentPagerAdapter不能在 FragmentPagerAdapter 中使用方法“getString(int resId)”
【发布时间】:2013-11-15 17:10:37
【问题描述】:
  1. 我的片段活动中有以下类,现在我想从 strings.xml 获取选项卡的名称,但我无法使用方法 getString(int resId) 它给出错误消息“无法对来自 Context" 类型的非静态方法 getString(int)。
  2. 在方法public CharSequence getPageTitle(int position) 我需要得到这样的name = getString(R.string.mystring) 任何帮助表示赞赏。
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;
          }
      }

【问题讨论】:

  • 您需要活动上下文才能调用getString。你的AppSectionsPagerAdapter也是一个内部类吗?
  • @Raghunandan 怎么做

标签: java android android-fragmentactivity


【解决方案1】:

您需要一个Context 对象来调用getResources().getString(int resID)。尝试在构造函数中传递 Context 并在这里使用它,就像

Context mContext;

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

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

【讨论】:

    【解决方案2】:

    如果static 是内部类,您可以从类定义中删除它。

    编辑:不要这样做——请参阅下面的评论。

    【讨论】:

    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多