【问题标题】:Junk after document element and root element must be well formed error文档元素和根元素后的垃圾必须是格式正确的错误
【发布时间】:2013-09-02 01:13:28
【问题描述】:

我从一本名为《Android Programming The Big Nerd Ranch Guide》的书中开始学习 Android 编程,这些就是其中一个示例。但是,我遇到了错误。
错误是:

  • 1。解析 XML 时出错:文档元素后出现垃圾。
  • 2。文档中根元素之后的标记必须格式正确。
  • 3.在布局文件中发现意外的文本:“””
我已经检查了这本书的勘误表,但是这段代码没有显示任何内容。 帮助将不胜感激
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".QuizActivity" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

<!-- Error 1 and 2 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:padding="24dp"
        android:text="@string/question_text" />

    <LinearLayout
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            <!-- Error 3 -->
        android:text="@string/true_button" />

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button" />

    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 对于第三个错误,如果你只是有 &lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button" /&gt; ,那么它也显示错误?
  • 是的。我刚刚在 stackoverflow 上添加了注释以显示错误在哪里。
  • 查看答案并评论任何进一步的问题。

标签: android


【解决方案1】:
  1. 解析 XML 时出错:文档元素后出现垃圾。
  2. 文档中根元素之后的标记必须格式正确。

这是因为您在 XML 布局文件中只能有一个根元素。所以从那里删除第二个根元素。

你有

<RelativeLayout>
...
</RelativeLayout>

<LinearLayout >
...
</LinearLayout>

两者都在您的根文件中。根目录下只能有一个布局。其余的应该嵌套在根 Layout 元素下。

3.在布局文件中发现意外的文本:“””

对于第三个错误,您可能需要查看答案 Strange Lint Warning - Unexpected text found in layout file: "" 。基本上,如果您在删除错误后执行Build-&gt;Clean,它应该会删除该错误。否则,空白处还有其他字符。然后您可能想再次编写 xml 文件,而不是复制粘贴它。

【讨论】:

  • XML 不会忽略多余的空格?我刚刚删除了两个按钮之间的空间并清除了错误
  • 有时您需要注意可能在行和空行之间的一些小字符。
猜你喜欢
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多