错误:"解析XML时出错:XML或文本声明不在实体的开头"

Ziz*_*eeo 22 eclipse android

我正在制作一个Sudoku 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>
Run Code Online (Sandbox Code Playgroud)

`

Zia*_*Zia 73

可能有两种情况 -

情况1 - 如果在第一个语句之前有空格.

情况2 - 如果您意外地将相同的命名空间声明放两次,即 - ?xml version ="1.0"encoding ="utf-8"?

我做了一次,并在纠正它之后降低了你的同样错误我的代码运行正常.希望这可以帮助


Kev*_*OUX 13

在某些情况下,如果您的文件顶部有两个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>
Run Code Online (Sandbox Code Playgroud)

保持一个,没关系


Rud*_*sop 7

有一个间距,这个"✗"面前 <?xml version="1.0" encoding="utf-8"?>

拿出来,它会工作.


小智 6

大多数情况下,"Error Parsing XML"错误归因于"Empty Space".这使JVM无法正确地膨胀Activity中的视图元素.所以,我建议....为了避免这种情况,不要手动搜索空格,请执行以下操作: -

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

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

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


THe*_*per 1

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

android:layout_Width
Run Code Online (Sandbox Code Playgroud)

代替

android:layout_width 
Run Code Online (Sandbox Code Playgroud)

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

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