2016-11-12 149 views
1

我想學習Android開發。作爲一個學習練習,我正在嘗試開發一個簡單的計算器應用程序。我的UI就緒時,它完美地呈現在Android Studio中,但是當過我試圖運行應用程序的機器人工作室給我如何解決在android開發中解析XML錯誤?

Error:(189) Error parsing XML: not well-formed (invalid token)

從互聯網的幫助下,我發現,如果我有任何的拼寫錯誤,它可能發生。但我在代碼中找不到任何內容。我重建了該項目,但錯誤仍然存​​在。我的代碼在下面,請幫我找到我哪裏錯了。

<?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:orientation="vertical" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.ahnaf.awsomecalc.MainActivity" 
    android:background="#373D4D"> 


    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="73dp" 
     android:background="#7069F2"> 
     <TextView 
      android:id="@+id/result" 
      android:text="Result" 
      android:textColor="#FFFFFF" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:gravity="right" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="73dp"> 
     <TextView 
      android:id="@+id/number" 
      android:text="Number" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="right" 
      android:textColor=" #373D4D" 
      android:background="#FFFFFF"/> 
    </LinearLayout> 


    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#373D4D"> 

     <Button 
      android:text="+" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/plus" 
      android:layout_weight="1" 
      /> 

     <Button 
      android:text="-" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/minus" 
      android:layout_weight="1" /> 

     <Button 
      android:text="=" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/equals" 
      android:layout_weight="1" /> 

     <Button 
      android:text="/" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/divide" 
      android:layout_weight="1" /> 

     <Button 
      android:text="*" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/multiply" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#373D4D"> 
     <Button 
      android:text="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/one" 
      android:layout_weight="1" /> 

     <Button 
      android:text="2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/two" 
      android:layout_weight="1" /> 

     <Button 
      android:text="3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/three" 
      android:layout_weight="1" /> 
    </LinearLayout> 



    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#373D4D"> 
     <Button 
      android:text="4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/four" 
      android:layout_weight="1" /> 
     <Button 
      android:text="5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/five" 
      android:layout_weight="1" /> 

     <Button 
      android:text="6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/six" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#373D4D"> 

     <Button 
      android:text="7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/seven" 
      android:layout_weight="1" /> 

     <Button 
      android:text="8" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/eight" 
      android:layout_weight="1" /> 

     <Button 
      android:text="9" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/nine" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#373D4D" 
     > 
     <Button 

      android:text="." 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/dot" 
      android:layout_weight="1" /> 

     <Button 
      android:text="0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/zero" 
      android:layout_weight="1" /> 

     <Button 
      android:text="<<" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/curr" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</LinearLayout> 

回答

1

您有2個問題。
第一招:在textColor屬性中刪除空格:

<TextView 
      android:id="@+id/number" 
      android:text="Number" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="right" 
      android:textColor="#373D4D" 
      android:background="#FFFFFF"/> 

第二個是:

<Button 
    android:text="<<" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/curr" 
    android:layout_weight="1" /> 

你應該提取字符串<<在編譯造成的問題。提取字符串資源,像這樣:

<Button 
     android:text="@string/yourstring" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/curr" 
     android:layout_weight="1" /> 

然後在strings.xml(/res/values/strings.xml)添加創建的資源:

<string name="yourstring"><![CDATA[<<]]></string> 
+0

thanks.it解決了這個問題。如何知道哪一行導致xml解析錯誤。我是一個java的傢伙。試圖學習android。有沒有任何IDE可以幫助我發現XML錯誤? – Ahnaf

+0

@Annaf Android Studio應該會有所幫助。棉絨通常檢測問題。 –

+0

文通過整個XML,+1:P –