6
  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;
          }
      }
Nitin Misra
  • 4,472
  • 3
  • 34
  • 52

2 Answers2

16

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
      }

and then use mContext.getResources().getString(int resID)

Apoorv
  • 13,470
  • 4
  • 27
  • 33
0

You can just remove the static keyword from your class definition if it's an inner class.

EDIT: Don't do this--see comment below.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • 2
    Avoid non-static inner classes in an activity if you don’t control their life cycle, use a static inner class and make a weak reference to the activity inside. http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-android/ – Raghunandan Nov 15 '13 at 17:17