2013-04-17 46 views
0

我剛剛開始了android上的初學者教程,編譯時我得到了4個錯誤,即使這些代碼基本上是從網頁上覆制的,並且我已經遵循了所有步驟。錯誤如下:android初學者教程錯誤解析XML

  • 元素類型「LinearLayout」必須後跟任一屬性規範,「>」或「/>」。 activity_main.xml/test/res/layout line 6 Android XML格式問題
  • R無法解析爲變量MainActivity.java/test/src/com/example/test line 12 Java問題
  • R無法解析爲一個變量MainActivity.java/test/src/com/example/test行19 Java問題
  • 錯誤:解析XML時出錯:格式不正確(無效標記)activity_main.xml/test/res/layout line 6 Android AAPT問題]

下面是代碼: activity_main.xml中

<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_width="0dp" 
      android:layout_height="wrap_content" 
      android:hint="@string/edit_message" 
      android:layout_weight="1" /> 

     <Button android:layout_width="wrap content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_send" /> 

    </LinearLayout> 

MainActivity.java

package com.example.test; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 

    public class MainActivity extends Activity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
     } 


     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.main, menu); 
      return true; 
     } 

    } 

我到處找,都試過了,它仍然是行不通的,所以這是我最後的希望。謝謝!

+1

一個很好的開發習慣是使用空白。看看我如何編輯你的例子,以及它如何讓錯誤更容易看到? – Simon

+0

我實際上最初使用過白色空間,但我擔心可能使用它是導致錯誤的原因。感謝您的建議! – user2291950

回答

0
<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_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" 
     android:layout_weight="1" /> 
    <Button android:layout_width="wrap content" 
     android:layout_height="wrap_content" 
     android:text="@string/button_send" /> 
</LinearLayout> 

拷貝,清潔工程和運行。您沒有在EditText之前結束LinearLayout標籤。

+0

非常感謝,解決了問題! – user2291950

2

您的LinearLayout標籤應該關閉。

<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" <-- close here 

這樣

<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" > 
+0

標記此正確。 – Akyl

+0

非常感謝,完全不顧我的注意 – user2291950