Android: 无法从页面适配器中的 id 找到资源(字符串数组)
Android: cannot find Resource (string-array) from id in page adapter
我制作了一个应用程序,其中有一个 ViewPager 与我制作的 PagerAdapter 的子class 一起工作,一切正常,但一些字符串是硬编码的,我决定将它们放在资源中,但是这带来了一些问题,当我尝试为我的 PagerAdapter 的 "getPageTitle" 方法检索字符串数组时出现此错误。
错误:
android.content.res.Resources$NotFoundException: String array resource ID #0x7f030002
我的 PagerAdapter 中的 getPageTitle :
@Nullable
@Override
public CharSequence getPageTitle(int position) {
String[] tabTitles = Resources.getSystem().getStringArray(R.array.days_short_names);
return tabTitles[position];
}
但是资源确实存在,这里是 string_arrays.xml :
<string-array name="days_short_names">
<item>@string/monday_short</item>
<item>@string/tuesday_short</item>
<item>@string/wednesday_short</item>
<item>@string/thursday_short</item>
<item>@string/friday_short</item>
<item>@string/saturday_short</item>
<item>@string/sunday_short</item>
</string-array>
加上strings.xml中的字符串:
<string name="monday_short">Mon</string>
<string name="tuesday_short">Tue</string>
<string name="wednesday_short">Wed</string>
<string name="thursday_short">Thu</string>
<string name="friday_short">Fri</string>
<string name="saturday_short">Sat</string>
<string name="sunday_short">Sun</string>
我什至检查了 R class,它就在那里,所以我有点不知道为什么它不起作用。
在 R.java 中:
public static final class array {
public static final int context_menu_actions=0x7f030000;
public static final int days_long_names=0x7f030001;
public static final int days_short_names=0x7f030002;
}
我猜你正试图从另一个 class 访问数组,而不是 activity,所以你使用:Resources.getSystem().getStringArray(R.array.days_short_names);
如果可以获取 activity 的上下文或从 getApplicationContext() 作为参数传递给 class 的构造函数并使用:
context.getResources().getStringArray(R.array.days_short_names);
我制作了一个应用程序,其中有一个 ViewPager 与我制作的 PagerAdapter 的子class 一起工作,一切正常,但一些字符串是硬编码的,我决定将它们放在资源中,但是这带来了一些问题,当我尝试为我的 PagerAdapter 的 "getPageTitle" 方法检索字符串数组时出现此错误。
错误:
android.content.res.Resources$NotFoundException: String array resource ID #0x7f030002
我的 PagerAdapter 中的 getPageTitle :
@Nullable
@Override
public CharSequence getPageTitle(int position) {
String[] tabTitles = Resources.getSystem().getStringArray(R.array.days_short_names);
return tabTitles[position];
}
但是资源确实存在,这里是 string_arrays.xml :
<string-array name="days_short_names">
<item>@string/monday_short</item>
<item>@string/tuesday_short</item>
<item>@string/wednesday_short</item>
<item>@string/thursday_short</item>
<item>@string/friday_short</item>
<item>@string/saturday_short</item>
<item>@string/sunday_short</item>
</string-array>
加上strings.xml中的字符串:
<string name="monday_short">Mon</string>
<string name="tuesday_short">Tue</string>
<string name="wednesday_short">Wed</string>
<string name="thursday_short">Thu</string>
<string name="friday_short">Fri</string>
<string name="saturday_short">Sat</string>
<string name="sunday_short">Sun</string>
我什至检查了 R class,它就在那里,所以我有点不知道为什么它不起作用。
在 R.java 中:
public static final class array {
public static final int context_menu_actions=0x7f030000;
public static final int days_long_names=0x7f030001;
public static final int days_short_names=0x7f030002;
}
我猜你正试图从另一个 class 访问数组,而不是 activity,所以你使用:Resources.getSystem().getStringArray(R.array.days_short_names);
如果可以获取 activity 的上下文或从 getApplicationContext() 作为参数传递给 class 的构造函数并使用:
context.getResources().getStringArray(R.array.days_short_names);