gpt4 book ai didi

java - Android Studio抛出 "android.content.res.Resources$NotFoundException"

转载 作者:行者123 更新时间:2023-12-02 09:16:34 30 4
gpt4 key购买 nike

您好,我正在使用 Android Studio,我遇到了一个简单但奇怪的问题,我似乎无法解决。我已将两个字符串数组定义为 xml 资源文件,如下所示。

我有一个充当 Controller 的单例类,我尝试使用 xml 文件中定义的字符串数组填充两个字符串数组列表。编译器似乎对编写的代码没有任何问题,甚至很乐意提供这两个字符串数组作为自动完成的一部分。只有当代码运行时我才会收到以下错误

android.content.res.Resources$NotFoundException

此时我已经尝试清理和重建项目,使缓存无效并重新启动AS,并且我还尝试重命名xml中的字符串数组并重新编译代码。最初,我创建了一个新的 xml 资源文件来存储这些字符串数组,但目前我已将它们移至字符串资源文件中,但无论哪种方式都不起作用。

这些都不起作用,我只是对问题所在感到非常困惑。我很感谢你们能提供的任何帮助。提前致谢。

这是字符串 xml 资源文件的相关部分。

<resources>
<string-array name="randomTriviaQuestionsArray">
<item>As far as has ever been reported, no-one has ever seen an ostrich bury its head in the sand.</item>
<item>Approximately one quarter of human bones are in the feet.</item>
<item>Popeye’s nephews were called Peepeye, Poopeye, Pipeye and Pupeye.</item>
<item>In ancient Rome, a special room called a vomitorium was available for diners to purge food in during meals.</item>
<item>The average person will shed 10 pounds of skin during their lifetime.</item>
<item>Sneezes regularly exceed 100 m.p.h.</item>
<item>A slug’s blood is green.</item>
<item>The Great Wall Of China is visible from the moon.</item>
<item>Virtually all Las Vegas gambling casinos ensure that they have no clocks.</item>
<item>The total surface area of two human lungs have a surface area of approximately 70 square metres.</item>
</string-array>

<string-array name="randomTriviaAnswersArray">
<item>True.</item>
<item>True – 52 bones in the feet and 206 in the whole body.</item>
<item>True.</item>
<item>False – It was the name for the entranceway to a stadium, nothing more.</item>
<item>False – they will shed approximately 40.</item>
<item>True</item>
<item>True</item>
<item>False – at a low orbit the Great Wall is visible, but no man-made structure is visible from outer space or the moon.</item>
<item>True</item>
<item>True</item>
</string-array>
</resources>

这是运行时发生错误的代码段。

public void setupQuestionBank(String questionFileName){
List<String> qList;
List<String> aList;

switch(questionFileName){
case "randomTrivia":
qList = Arrays.asList(Resources.getSystem()
.getStringArray(
R.array.randomTriviaQuestionsArray));

aList = Arrays.asList(Resources.getSystem()
.getStringArray(
R.array.randomTriviaAnswersArray));
break;
default:
qList = new ArrayList<>();
aList = new ArrayList<>();
}

编辑:这是此问题的解决方案,感谢 DevMike01。使用 Activity 或 Fragment 的 Context 对象来访问资源。

public void setupQuestionBank(String questionFileName){
List<String> qList;
List<String> aList;

switch(questionFileName, Context context){
case "randomTrivia":
qList = Arrays.asList(context.getResources()
.getStringArray(
R.array.randomTriviaQuestionsArray));

aList = Arrays.asList(context.getResources()
.getStringArray(
R.array.randomTriviaAnswersArray));

break;
default:
qList = new ArrayList<>();
aList = new ArrayList<>();
}

最佳答案

您以错误的方式引用字符串数组。替换Resources.getSystem()getActivity().getResources()如果您在 fragment 中,但如果代码在 Activity 中,只需调用 getResources()直接地。最后你的代码应该是这样的 ...getResources().getStringArray(...);/

关于java - Android Studio抛出 "android.content.res.Resources$NotFoundException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58942061/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com