FragmentPagerAdapterでメソッド "getString(int Resight)"を使用できません
-
20-12-2019 - |
質問
- 私のフラグメントアクティビティに次のクラスは、strings.xmlからタブの名前を取得したいですが、メソッド
getString(int resId)
を使用できません "エラーメッセージが表示されません"タイプコンテキストからの静的メソッドgetString(int)。 - メソッド
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; } }
解決
Context
を呼び出すためのgetResources().getString(int resID)
オブジェクトが必要です。コンストラクタにContext
を渡して、ここで
Context mContext;
public AppSectionsPagerAdapter(FragmentManager fm,Context context) {
super(fm);
mContext = context
}
.
を使用してmContext.getResources().getString(int resID)
他のヒント
内部クラスである場合は、クラス定義からstatic
キーワードを削除するだけです。
編集:これをしないでください - 下記のコメントを参照してください。
所属していません StackOverflow