【问题标题】:Java Android android content res Resources NotFoundException:Java Android android 内容资源 NotFoundException:
【发布时间】:2014-07-02 05:08:35
【问题描述】:

我开发了一个 Java Android 应用程序,但我遇到了问题 :)

我想加载一个 XML 文件。

AssetManager assetManager = getAssets();
InputStream questionsFile = assetManager.open("questions.xml");
document = builder.parse(questionsFile);
NodeList nList = document.getElementsByTagName("question");

XML 位于 Asset 文件夹中,但我得到一个 res Resources NotFoundException。

感谢您的帮助!

这是我的 XML 文件:

<questions>
    <question text='Qui est le réalisateur de blade runner ?'>
        <answer trueAnswer='false'>Stanley Kubrick</answer>
        <answer trueAnswer='false'>Steven Spielberg</answer>
        <answer trueAnswer='true'>Ridley Scott</answer>
        <answer trueAnswer='false'>Roland emmerich</answer>
    </question>
    <question text='Ou se situe le siège social de la société Facebook ?'>
        <answer trueAnswer='true'>Palo Alto</answer>
        <answer trueAnswer='false'>Philadelphie</answer>
        <answer trueAnswer='false'>Dallas</answer>
        <answer trueAnswer='false'>Boston</answer>
    </question>
    <question text='Combien de saison a durée la série X files ?'>
        <answer trueAnswer='false'>1</answer>
        <answer trueAnswer='false'>5</answer>
        <answer trueAnswer='true'>7</answer>
        <answer trueAnswer='false'>9</answer>
    </question> 
</questions>

实际上我的 XML 正在加载,但是这段代码:

NodeList nList = document.getElementsByTagName("question");
Toast.makeText(getBaseContext(),nList.getLength(), 
                    Toast.LENGTH_LONG).show();

只能在 Java 中工作,不能在 android 中工作。

感谢大家的回答

/////////////////////////////////////// ////////

它的工作,这个代码:

AssetManager assetManager = getAssets();
document =    builder.parse(assetManager.open("XML/questions.xml"));
NodeList nList = document.getElementsByTagName("question");

textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
                  textViewQuestion.setText(nList.item(0).getAttributes().getNamedItem("text").getNodeValue());

【问题讨论】:

  • 能否请您发布您的 questions.xml 代码?
  • 我已经测试了你的代码并且它正在工作我认为你的 questions.xml 文件有问题你能发布 questions.xml 文件的内容吗?
  • 你会发布你的 LogCat 或堆栈跟踪吗?

标签: java android xml android-assets


【解决方案1】:

我也遇到过这个问题,在asset中创建子目录,把这个文件放到子目录下,给路径assetManager.open("subDir/questions.xml");

它会工作..检查它..

【讨论】:

    【解决方案2】:

    清理并构建应用程序,然后重新启动您的应用程序

    【讨论】:

    • 我已经清理了重建应用程序,但我得到了 E/AndroidRuntime(1962): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3 error again
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多