【问题标题】:Android Studio throwing "android.content.res.Resources$NotFoundException" [duplicate]Android Studio 抛出“android.content.res.Resources$NotFoundException”[重复]
【发布时间】:2019-11-20 05:21:04
【问题描述】:

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

我有一个作为控制器的单例类,我尝试使用 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<>();
        }

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    您以错误的方式引用字符串数组。如果您在片段中,请将Resources.getSystem() 替换为getActivity().getResources(),但如果代码在活动中,则只需直接调用getResources()。最后你的代码应该看起来像这样...getResources().getStringArray(...);/

    【讨论】:

    • 好的,我明白了。这个类既不是活动也不是片段,它是一个充当控制器的单例。试图找到一种方法来引用这个类中的这两个字符串数组实际上是导致我找到Resources.getSystem() 位的原因,但显然这是一个错误。为了做你所说的,我修改了方法并要求调用活动的上下文作为附加参数。从那里我听从了你的建议并使用context.getResources(),它似乎工作,所以谢谢你!快速跟进,使用上下文而不是 getActivity() 有什么危害吗?
    • 嗯好吧,在那种情况下,我很好奇为什么一个给出错误而另一个没有。除非您的意思是 getActivity().getResources()context.getResources() 之间没有区别。是的,这正是我正在做的,我已经修改了方法,以便它要求传入上下文。
    • 在大多数情况下,只要您不将 context/getActivity() 设为静态,就没有副作用。由于它是一个单例,您可能正在从拥有它的某个地方注入context。这与在拥有该上下文的类中调用 getResources() 相同。
    • 好的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多