解析XML时出错:格式不正确(无效令牌)

时间:2012-06-17 05:20:03

标签: android eclipse parsing

我是java编程的新手,现在严格遵循一个教程。我已经深入了解它,直到发生了我无法解决的事情。我搜索谷歌,一遍又一遍地看着代码,没有看到任何错误。这是我的代码的截图.... nvm我不允许发布图片因为我是新的所以这里是复制的代码。第一个X表示错误解析XML:格式不正确(无效令牌),第二个X表示XML文档结构必须在同一个实体内开始和结束。感谢所有的帮助,谢谢。

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myMessage"
        android:text="@string/hello" />

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

< LinearLayout--------1st X is located here. The < is not spaced in the code but spaced here because that was the only way i could put it here
    android:layout_width="fill_parent"
    android:layout_height="20dp"
    android:orientation="horizontal">


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#ff0000"
        android:textColor="#000000"
        android:text="red" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#00ff00"
        android:textColor="#000000"
        android:text="green" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#0000ff"
        android:textColor="#000000"
        android:text="blue" />

< LinearLayout >---------2nd X is located here. The < is not spaced in the code but spaced here because that was the only way i could put it here and there is a / in between < and LinearLayout

4 个答案:

答案 0 :(得分:1)

xml结构不合理......下面是更正的。 &lt;之间不应该有空格。 LinearLayout&amp;此标记未正确关闭。

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myMessage"
    android:text="@string/hello" />

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

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="20dp"
android:orientation="horizontal">


<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:background="#ff0000"
    android:textColor="#000000"
    android:text="red" />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:background="#00ff00"
    android:textColor="#000000"
    android:text="green" />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:background="#0000ff"
    android:textColor="#000000"
    android:text="blue" />

</LinearLayout >
</LinearLayout >

答案 1 :(得分:0)

您错过了底部的一个关闭代码</LinearLayout>。在这里工作一个`

<TextView
    android:id="@+id/myMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="20dp"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ff0000"
        android:gravity="center"
        android:text="red"
        android:textColor="#000000" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#00ff00"
        android:gravity="center"
        android:text="green"
        android:textColor="#000000" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#0000ff"
        android:gravity="center"
        android:text="blue"
        android:textColor="#000000" />
</LinearLayout>

`

答案 2 :(得分:0)

您不能在开放标记和标记名称之间留出空格。给出没有空格的线性布局标签,如

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" >

这是错误的

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">

答案 3 :(得分:0)

<LinearLayout代码中删除空格。使用<LinearLayout代替< LinearLayou

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="20dp"
    android:orientation="horizontal">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myMessage"
        android:text="@string/hello" />

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#ff0000"
        android:textColor="#000000"
        android:text="red" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#00ff00"
        android:textColor="#000000"
        android:text="green" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#0000ff"
        android:textColor="#000000"
        android:text="blue" />
</LinearLayout>
</LinearLayout>