【问题标题】:Eclipse Errors: Error parsing XML: not well-formed (invalid token)Eclipse 错误:解析 XML 时出错:格式不正确(令牌无效)
【发布时间】:2014-11-14 03:11:00
【问题描述】:

这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>
LinearLayout 
    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"
    android:orientation="horizontal"
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message";
        </EditText>
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send";
       </Button>
</LinearLayout>

有一个错误 在第一个 EditText 和第一个 Button 行,第一个错误是标题中的错误,第二个错误是: '元素类型'EditText'必须后跟属性规范,'>'或'/>'

怎么了?整个代码(几乎)完全按照此处所示http://developer.android.com/training/basics/firstapp/building-ui.html

(如果我拼写错误或忘记了什么,我很抱歉,我在这里是菜鸟......)

编辑:谢谢,伙计们,但我尝试将您的代码粘贴到我的代码上。虽然没有 X 标志,但调试工具仍然说有一些错误...

编辑:是的,我有所需的字符串,因为我按照教程进行操作。但我不明白...如果我将教程网站上给出的代码粘贴到 Eclipse 中,仍然会出现错误!我会尝试在另一个程序中打开 .xml。

【问题讨论】:

  • 如果您没有相应的字符串,错误也可能来自 android:text。我编辑了我的答案。
  • 尝试使用常规 XML 编辑器打开文件,看看它是否告诉您任何信息。

标签: android eclipse


【解决方案1】:

您必须在&lt;EditText key="value" /&gt; 中使用/&gt; 或在&lt;EditText key="value"&gt;&lt;/EditText&gt; 中使用&gt; 关闭您的标签。不要使用;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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"
    android:orientation="horizontal">

    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" >
    </EditText>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

编辑:之后,如果仍有错误,请删除:

android:hint="@string/edit_message"

android:text="@string/button_send"

除非您的 values/strings.xml 文件中有相应的条目:

<string name="edit_message">Enter a text</string>
<string name="button_send">Button text</string>

【讨论】:

  • 抱歉,这个布局是格式良好的 XML。复制/粘贴,你会看到
  • 我做过一次,现在又试了一次,它起作用了:D YAY!非常感谢! (你会在这个网站上再次收到我的消息,因为我通常在编程方面遇到问题:P)
  • 以防万一还没有被理解,someone755和Michel-F的交流。 Portzert 暗示了一个有用的提示:如果一切似乎正确并且错误仍然存​​在,则突出显示并剪切 xml 页面的违规部分,保存然后将它们复制回来。(荒谬,我知道,但它对我有用......)
【解决方案2】:

这是它的外观(注意以“

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

android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"  
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
</Button> 
</LinearLayout>

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多