Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 资源字符串作为tabLayout的标题_Java_Android_Arrays_String - Fatal编程技术网

Java 资源字符串作为tabLayout的标题

Java 资源字符串作为tabLayout的标题,java,android,arrays,string,Java,Android,Arrays,String,因此,我有以下问题: 我想使用字符串数组作为TabLayout的标题 在本例中,我将ViewPagerAdapter用于布局,现在我想将选项卡的标题保存在字符串数组中 为了提供这些标题的翻译,我使用了2个资源字符串,但我不能使用它们,因为字符串数组只能使用字符串,而资源字符串是int(完美句子) 不兼容的类型 必需:java.lang.String 找到:int 错误在这里: String[] tabTitleArray = {R.string.dice, R.string.presets

因此,我有以下问题: 我想使用字符串数组作为TabLayout的标题

在本例中,我将ViewPagerAdapter用于布局,现在我想将选项卡的标题保存在字符串数组中

为了提供这些标题的翻译,我使用了2个资源字符串,但我不能使用它们,因为字符串数组只能使用字符串,而资源字符串是int(完美句子)

不兼容的类型

必需:java.lang.String

找到:int

错误在这里:

   String[] tabTitleArray = {R.string.dice, R.string.presets};

另外:我不能使用
getResource().getString()

改用字符串数组:

<string-array name="labels">
   <item>my URL1</item>
   <item>my URL2</item>
</string-array>

请改用字符串数组:

<string-array name="labels">
   <item>my URL1</item>
   <item>my URL2</item>
</string-array>

一个直截了当的解决方案是为ViewPagerAdapter提供上下文,以便访问资源,如:

public ViewPagerAdapter (FragmentManager manager, Context context) {

    super(manager);
    this.mContext = context;
}
然后

mContext.getResource().getString(....)

一个直截了当的解决方案是为ViewPagerAdapter提供上下文,以便访问资源,如:

public ViewPagerAdapter (FragmentManager manager, Context context) {

    super(manager);
    this.mContext = context;
}
然后

mContext.getResource().getString(....)

为什么不将这些值转换为字符串而不是资源?如果您不能使用getResources(),则使用FragmentManager在适配器中传递字符串数组,并使用getResources()final Stringlabels=getApplicationContext().getResources().getString(R.string.labels)声明该字符串数组;为什么不将这些值转换为字符串而不是资源?如果您不能使用getResources(),则使用FragmentManager在适配器中传递字符串数组,并使用getResources()final Stringlabels=getApplicationContext().getResources().getString(R.string.labels)声明该字符串数组;工作正常,但如果在片段中使用方法getResources()use getContext()或getActivity()访问getResources().getStringArray(R.array.labels),则无法解析该方法getResources()use getContext()或getActivity()。或者在适配器中传递活动的上下文\在活动/片段中初始化字符串数组,并像Lino建议的那样将其传递到适配器的构造函数中:)谢谢您的帮助如果这是您的解决方案,您能接受答案吗?我不知道,所以只能猜测,但如果在片段中使用方法getResources()use getContext()或getActivity()来访问getResources().getStringArray(R.array.labels),则无法解析该方法getResources()use getContext()或getActivity()。或者在适配器中传递活动的上下文\在活动/片段中初始化字符串数组,并像Lino建议的那样将其传递到适配器的构造函数中:)谢谢您的帮助如果这是您的解决方案,您能接受答案吗?我不知道,只是guessing@2Simpel为了遵守SO规则,请接受其中一个已发布的答案或发布解决方案并接受it@2Simpel为了遵守SO规则,请接受其中一个已发布的答案或发布解决方案并接受它