2015-06-27 322 views
2

1.錯誤顯示在打開TextView標記的行上,但它一直移動到不同的行,所以我不確定。 2.另外,對不起,如果其書面只是不好,這是我在編程解析XML時出錯:不匹配的標記(無效標記)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:ide="@+id/text_prompt" 
     android:text="Enter the total bill amount" 
     android:textSize="20sp" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:ems="10" 
     android:inputType="numberDecimal" /> 

     </requestFocus> 
    <EditText/> 

    <Button 
     android:id="@+id/tip20" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="20 Percent" 
     android:layout_alignParentBottom="true" /> 

    <Button 
     android:id="@+id/tip15" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="50dp" 
     android:text="15 Percent" /> 

    <Button 
     android:id="@+id/tip10" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="100dp" 
     android:text="10 Percent" /> 

    <TextView 
     android:id="@+id/tip_prompt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:text="Select a tip amount:" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="150dp" /> 
</RelativeLayout> 

回答

1

這裏您有4條錯誤。

  1. 您已經關閉了根RelativeLayout的太早(5號線) - should be > instead of />

  2. 你已經關閉的EditText太早(第24行) - should be > instead of />

  3. 語法錯誤在第26行 - 應該<requestFocus /> instead of </requestFocus>

  4. 語法錯誤在第27行 - 應該是</EditText> instead of <EditText/>

正確的XML應該是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:ide="@+id/text_prompt" 
     android:text="Enter the total bill amount" 
     android:textSize="20sp" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:ems="10" 
     android:inputType="numberDecimal"> 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/tip20" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="20 Percent" /> 

    <Button 
     android:id="@+id/tip15" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="50dp" 
     android:text="15 Percent" /> 

    <Button 
     android:id="@+id/tip10" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="100dp" 
     android:text="10 Percent" /> 

    <TextView 
     android:id="@+id/tip_prompt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="150dp" 
     android:text="Select a tip amount:" 
     android:textSize="20sp" /> 
</RelativeLayout> 
+0

現在它給我一個垃圾文件元素後出錯 –

+0

@MattBoggs,編輯我的答案;) – localhost

+0

仍然是同樣的事情。我正在跟着一個教程,所以我不知道什麼是錯的 –

1

你沒有接近根部的RelativeLayout的標籤第一次嘗試。注意/>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" /> 

應該只是>

+0

我做第一的EditText是一回事嗎? –

+0

沒有。僅適用於ViewGroup的子類。例如。 RelativeLayour或LinearLayout,可以有孩子。 EditText不能有孩子。所以你可以立即關閉其標籤 – Blackbelt

0

你太早關閉相對根標籤。 你所要做的就是從/> 中刪除「/」結構通常是如何去除的 或者你可以做,但你不能做你試圖用你的代碼做什麼。它會在更正後編譯。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="10dp" >. </relativelayout> 
相關問題