【问题标题】:error: Error parsing XML: mismatched tag错误:解析 XML 时出错:标签不匹配
【发布时间】:2015-07-10 20:39:31
【问题描述】:

我收到此错误:

解析 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" >


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android" >

    <Button
        android:id="@+id/btnChangeImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Image" >

</LinearLayout>

【问题讨论】:

    标签: xml xml-parsing xml-validation


    【解决方案1】:

    &lt;ImageView&gt;&lt;Button&gt; 标记的结束标记丢失。

    您可以在标签末尾添加/ 以使其自动关闭:

    <?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" >
    
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/android" />
    
        <Button
            android:id="@+id/btnChangeImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change Image" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      同意上述答案,但我认为可以通过向您展示一种简单的方法来为将来自己调试此问题(教人钓鱼...)来添加答案。

      现代浏览器内置了 XML 解析器。使用 Internet Explorer 打开您的原始文件。它给出了这个错误信息:

      End tag 'LinearLayout' does not match the start tag 'Button'. 
      

      关闭 Button 节点后,保存文件并再次打开它。它给你这个错误:

      End tag 'LinearLayout' does not match the start tag 'ImageView'.
      

      关闭该标签,保存并重新打开文件。格式良好的 XML 在浏览器中呈现。虽然通常我不是 IE 的大力支持者,但它提供了比 Chrome 更好的错误消息,并且它允许您折叠和展开每个级别的嵌套 XML,这对于更复杂的 XML 非常有帮助。我希望这项技术在未来对您有所帮助。

      【讨论】:

      • 谢谢,这个建议确实帮助我调试了文档中缺少的 43K 行的结束标签。
      【解决方案3】:

      需要关闭ButtonImageView标签。

      对于您打开的每个标签/节点/元素,您还需要关闭它们。

      例如:如果您使用&lt;Tag&gt; 打开,则需要使用&lt;/Tag&gt; 关闭或在标签末尾添加一个斜杠,如下所示:&lt;Tag /&gt;

      您的问题的解决方案如下:

      <?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" >
      
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/android" /> <!-- This one here -->
      
          <Button
              android:id="@+id/btnChangeImage"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Change Image" /> <!-- This one here -->
      
      </LinearLayout>
      

      <?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" >
      
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/android" >
          </ImageView> <!-- This one here -->
      
          <Button
              android:id="@+id/btnChangeImage"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Change Image" >
          </Button> <!-- This one here -->
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2014-04-26
        • 2012-08-16
        • 2013-12-16
        • 1970-01-01
        • 2018-03-03
        • 2015-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多