【问题标题】:Error: "Error parsing XML: XML or text declaration not at start of entity"错误:“解析 XML 时出错:XML 或文本声明不在实体开头”
【发布时间】:2020-12-08 04:08:09
【问题描述】:

我正在制作一个数独 Android 应用程序,我在 main.xml 下收到以下错误:“错误:解析 XML 时出错:XML 或文本声明不在实体开头”任何帮助将不胜感激。这是我的代码。我在错误旁边放了“✗”

  `✗<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@String/continue_label"/>

        <Button
            android:id="@+id/continue_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/continue_label" />

        <Button
            android:id="@+id/new_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/new_game_label"/>

        <Button 
            android:id="@+id/about_button"
            android:layout_Width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/new_game_label"/>

        <Button
            android:id="@+id/exit_button"
            android:layout_Width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/exit_label"/>
    </LinearLayout>

`

【问题讨论】:

  • 细节,细节,细节……我们需要细节。
  • 您可以编辑您的原始帖子以包含尽可能多的必要/相关代码。编辑时,请使用可用于文本区域的格式化工具来格式化您的代码。您的问题越清晰易读,您将获得的帮助就越多。
  • 我不太清楚你的代码是什么,什么不是,但是在 XML 文件中的 "
  • 哦,很抱歉 ✗ 是为了告诉你错误在哪里,但之前什么都没有。

标签: android eclipse


【解决方案1】:

可能有两种情况-

案例 1 - 如果您在第一条语句之前有一个空格。

案例 2 - 如果您不小心将相同的命名空间语句放置了两次,即 - ?xml version="1.0" encoding="utf-8"?

我做了一次,在更正后遇到了同样的错误,我的代码运行良好。希望这会有所帮助

【讨论】:

    【解决方案2】:

    在某些情况下,如果我的文件顶部有两个 XML 版本( xml version="1.0" encoding="utf-8" ),则会出现此错误:

    <?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        ...
    
    </shape>
    

    留一个就可以了

    【讨论】:

    • 我遇到了同样的问题。就我而言,我的布局中有另一行 &lt;?xml version="1.0" encoding="utf-8"?&gt; (不是 xml 布局中的第一行)。
    【解决方案3】:

    &lt;?xml version="1.0" encoding="utf-8"?&gt;前面有一个空格和这个“✗”

    把它拿出来就可以了。

    【讨论】:

      【解决方案4】:

      大多数情况下,错误“解析 XML 错误”是由“空白空间”引起的。 这使得 JVM 不能正确地膨胀 Activity 中的视图元素。所以,我建议......为了避免这种情况,而不是手动搜索空格,请执行以下操作:-

      步骤 1. Ctrl +A -> 选择 XML 中的所有代码。

      步骤 2. Ctrl+I -> 自动缩进 XML 中的代码

      (**以上快捷键适用于 Eclipse IDE)

      【讨论】:

        【解决方案5】:

        我能看到你的 xml 的唯一问题是你写了两次

        android:layout_Width
        

        而不是

        android:layout_width 
        

        除此之外,您的 xml 看起来还不错。 就像 Matthew Wilson 建议的那样,确保 xml 声明前面没有任何内容

        如果这不起作用,请尝试创建一个新的 xml 文件(在 Eclipse 中,通过菜单 File > New -> Android XML File。)然后逐个添加现有 xml 部分的部分,每次检查它是否仍然有效.这样您就可以查明问题所在。

        【讨论】:

        • 是的,大概是关于和退出按钮上的大写W!
        • 哦,是的,我明白了,谢谢。但是我这里仍然有2个错误:不允许匹配“[xX][mM][lL]”的处理指令目标。错误 2:错误:解析 XML 时出错:XML 或文本声明不在实体开头
        • 不需要清理项目(但也不会造成伤害)。查看我修改后的答案,了解解决问题的替代步骤。
        【解决方案6】:

        在我的情况下,在某些 info.plist 文件中,第一行有一个空行导致此问题...检查 info.plist 是否有额外的空行,如果有,请将其删除。

        更多信息: https://github.com/ydkhatri/mac_apt/issues/16

        【讨论】:

          【解决方案7】:

          其中一个可能的原因可能是您在 xml 文件中的另一个命名空间中声明了命名空间。例如 `

            <?xml version="1.0" encoding="utf-8"?>
               <selector xmlns:android="http://schemas.android.com/apk/res/android">
                  <?xml version="1.0" encoding="utf-8"?>
                   <shape xmlns:android="http://schemas.android.com/apk/res/android"
                     android:shape="rectangle" >
          
                     <corners
                        android:bottomLeftRadius="30dp"
                        android:topLeftRadius="30dp" />
          
                   <solid android:color="#CFCFCF" />
          
                   <padding
                  android:bottom="0dp"
                  android:left="0dp"
                  android:right="0dp"
                  android:top="0dp" />
          
                 <size
                  android:height="60dp"
                  android:width="130dp" />
          
               </shape>
            </selector>
          

          `

          【讨论】:

            猜你喜欢
            • 2014-02-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-08-03
            • 1970-01-01
            • 2011-12-30
            • 2013-11-13
            • 2011-11-06
            相关资源
            最近更新 更多