1. 程式人生 > >xml中的Premature end of file

xml中的Premature end of file

  寫Android的時候想自己寫個佈局檔案,索性就準備把自己生成的給全部註釋掉,程式碼如下:

<!--  
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
-->

  結果第一行就有一個讓程式設計師恐懼的一個東西:紅叉。。。。

  如果把游標移動紅叉上面就提示Premature end of file.

  而且控制檯會輸出:Error in an XML file: aborting build.

  上網百度了一下,發現沒有找到我想要的答案,於是自己就開始亂搗鼓,結果還居然可以了,雖然不明覺厲

  其實我只是簡單地修改一下而已

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
<!--  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
-->
</RelativeLayout>
 

  對比了一下兩者的區別,發現不同的是標籤的包含範圍,前者是<!-- -->中包含了全部,後者是<!-- -->包含了部分。

  所以個人由此YY一個結論:<!-- -->的開始部分<!-- 不能放於檔案的開頭。

  剛才再次測試了一下,發現如果<!-- -->的前面僅僅是<?xml version="1.0" encoding="utf-8"?>,那麼也會報相同的錯誤。

  有了這個YY, 驗證起來也就簡單多了,所以也算是解決了。

轉載請註明原文地址,謝謝~