1

I've started learning Android programming from a book called Android Programming The Big Nerd Ranch Guide and these were one of the examples. However, I am getting errors.
The errors are:

  • 1. Error parsing XML: junk after document element.
  • 2. The markup in the document following the root element must be well-formed.
  • 3. Unexpected text found in layout file: """
I have checked the errata for this book but nothing is shown for this segment of code. Help would be appreciated
<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"
    tools:context=".QuizActivity" >

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

</RelativeLayout>

<!-- Error 1 and 2 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:padding="24dp"
        android:text="@string/question_text" />

    <LinearLayout
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            <!-- Error 3 -->
        android:text="@string/true_button" />

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button" />

    </LinearLayout>

</LinearLayout>
LinhSaysHi
  • 642
  • 5
  • 14
  • 30

1 Answers1

1
  1. Error parsing XML: junk after document element.
  2. The markup in the document following the root element must be well-formed.

That's because you can only have one root element in a XML Layout file. So remove the second root element from there.

You have

<RelativeLayout>
...
</RelativeLayout>

and

<LinearLayout >
...
</LinearLayout>

both in your file at the root. You can only have one Layout at the root. Rest of them should be nested under the root Layout element.

3.Unexpected text found in layout file: """

For the third error you might want to look at the answers Strange Lint Warning - Unexpected text found in layout file: "" . Basically if you do a Build->Clean after removing the errors, it should remove that error. Else, there is some other character in the white space. You may want to then write the xml file again, without copy-pasting it.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • XML does not ignore extra whitespace? I just removed the space between the two buttons and the error cleared – LinhSaysHi Sep 02 '13 at 01:38
  • Sometimes you need to be careful of some small characters that might be in between the lines and the empty lines as well. – Shobhit Puri Sep 02 '13 at 01:54