How to get String Resource within ViewPager Adapter?

后端 未结 9 1832
孤城傲影
孤城傲影 2020-12-31 07:42

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. Coul

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 08:40

    You are trying to fetch String inside your Resources without gaining Resources. So try to pull your Resources with getResources() with your Context.

    If your are inside Activity or Service then context is there. If outside then try to pull Context first with getApplicationContext() and on that pull the resources and pull your String in the end on it.

    As per your Code these snippet would't throw any exception if resources and mentioned String id's are pulled properly.

     public CharSequence getPageTitle(int position) 
      {
        switch (position) 
        {
        case 0:
               return getResources().getString(R.string.title1);
        case 1:
               return getResources().getString(R.string.title1);
        case 2:
               return getResources().getString(R.string.title1);
         }
        return null;
      }
    

提交回复
热议问题