Android XML Error parsing XML: junk after document element - android

I am getting this error:
Multiple annotations found at this line:
- The markup in the document following the root element must be well-
formed.
- error: Error parsing XML: junk after document element
This is appearing on the second TextView in this xml file.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TextView>
<TextView android:id="#+id/text2"
android:textSize="12sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
></TextView>
<TextView android:id="#+id/text3"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
Any ideas on the mistake I am making?
All help is appreciated.

An XML document can only have 1 root element. You have 3 such elements. You should enclose your TextView elements inside a layout, such as a LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TextView elements here -->
</LinearLayout>
Remove the xmlns attribute on the first TextView element.

you should encapsule your elements in layouts, such as LinearLayout. You can look at a layout config file in existence.

Related

Junk after document element and root element must be well formed error

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>
Error parsing XML: junk after document element.
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.

Android XML error after updating eclipse

After recently updating eclipse i keep getting an error saying "Attribute name "android:tools" associated with an element type "RelativeLayout" must be followed by the ' = ' character.
It says it on line 9 but there isn't any android:tool there. I've tried cleaning and restarting eclipse and still no solution. I never changed any part of the file, and the funny thing is all my other activities are the same and don't have any errors. Any help would be appreciated thanks.
Edit... I fixed the problem by deleting the file and creating a new one. File must have been corrupt or something. Thanks for the input everyone, sorry for the run confusion.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="wrap_content"
android:background="#color/backtext"
tools:context=".Brakes"
android:screenOrientation="portrait" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/brakes1"
android:textColor="#color/textviews"
android:textSize="15sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:text="#string/brakes2"
android:textColor="#color/textviews"
android:textSize="15sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:text="#string/brakes3"
android:textColor="#color/textviews"
android:textSize="15sp"/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView4"
android:text="#string/brakes4"
android:textColor="#color/textviews"
android:textSize="15sp"/>
</RelativeLayout>
</ScrollView>
Try moving this line:
xmlns:tools="http://schemas.android.com/tools"
to your ScrollView instead of the RelativeLayout where it is now.
it looks like your RelativeLayout used to be your root view, and you added the ScrollView at some point later.
you are missing xmlns:tools="http://schemas.android.com/tools" in your root parent,please include it.
Remove xmlns:android="http://schemas.android.com/apk/res/android" from your RelativeLayout. There should be only one of those at the layout root element.

RelativeLayout textviews overlapping

I have a rather simple ListView row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="25dp" />
</RelativeLayout>
When the text of "#+id/tournament_name"is long - it overlaps with the one from "#+id/tournament_winner" and I don't understand why.
I tried using android:singleLine="false"to no avail. I also tried using android:inputType="textMultiLine"as the docu says android:singleLine="false" is deprecated but then I get the warning: Attribute android:inputType should not be used with <TextView>: Change element type to
<EditText> ? so no good here as well.
I also tried using android:ellipsize="end" but this doesn't work. I assume it is because the text in the left TextView ("#+id/tournament_name") is NOT long enough to fill up the full width of the ListView (which code is not sowing here).
I was sure that if I use android:layout_width="wrap_content"the two TextView fields shouldn't overlap.
Here is an example (see the second line):
Any further ideas how this could be fixed?
android:layout_toLeftOf="#id/tournament_winner" in First TextView.
Also set android:maxLines="1" and Fix width for tournament winner because when it gets long tournament name cant see...
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/tournament_winner"
android:maxLines="1"
android:text="NAMEEEEEEEEEEEEEEEEEEEEEEEEEEE"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content" android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="WINER"
android:textSize="25dp" />
</RelativeLayout>
Thank you very much for your answer - sorry it took me some time to respond. I ended up using your android:layout_toLeftOf="#+id/tournament_winner" but left the single line and the margin to the left unused, as the result seemed perfect to me (hope this is also the case for other devices).
One thing though - in the first text view (tournament_name) I had to use android:layout_toLeftOf="#+id/tournament_winner"and not android:layout_toLeftOf="#id/tournament_winner" - pay attention to the added +. For some reason I get an error using android:layout_toLeftOf="#id/tournament_winner": Error: No resource found that matches the given name... so it seems that it is possible and NEEDED to define the resource in the time of calling it because the system doesn't know it before it was defined.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tournament_winner"
android:layout_alignParentLeft="true"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="25dp" />
</RelativeLayout>
You can use single text view in place of two and simply display both strings in one text view !!

Relative Layout not compiling!

I was trying to do an adapter on a ListView as an exercise but I get a strange error on the row layout:
error: Error parsing XML: unbound prefix
What is wrong with this very simple Layout??!?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/station"
android:text="Stazione DI"
android:layout_alignParentTop="true"
android:padding="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
androdi:id="#+id/time"
android:text="Time:"
android:layout_alignParentLeft="true"
android:below="#id/station"/>
<TextView
androdi:id="+id/late"
android:text="Time:"
android:layout_toRightOf="#id/time"
android:below="#id/station"/>
<TextView
androdi:id="+id/rail"
android:text="Rail:"
android:below="#id/station"
android:layout_toRightOf="#id/late"/>
</RelativeLayout>
Prefixes are those strings you type before the :, like android:.... And you typed it wrong once (androdi:...)
(edit: actually 3 times)
Prefixes in Last TextView spell not correct, check that
In xml file last Textview is worong
<TextView
androdi:id="+id/rail"
android:text="Rail:"
android:below="#id/station"
android:layout_toRightOf="#id/late"/>
It will be
<TextView
android:id="+id/rail"
android:text="Rail:"
android:below="#id/station"
android:layout_toRightOf="#id/late"/>
you have written android spelling wrong in following cases !
androdi:id="#+id/time"
androdi:id="+id/late"
androdi:id="+id/rail"

How can i make use of views defined in layout XML file as template to create views programmatic way

I want to populate a table, defined in layout xml file through the programmatic way. I have define Table with a single row defining its header, with all the attributes set. Now i want to know a way so that i can just replicate that header row in the table with new content.
I tried using inflator inflate(int,view) method, but at runtime it showed up with error.
Here is the XML code for the layout file defining the table
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1,2"
android:id="#+id/tLayout"
>
<TableRow
android:layout_width="fill_parent"
android:id="#+id/tRow">
<TextView
android:id="#+id/name"
android:padding="3dip"
android:gravity="left"
android:text="Name"
/>
<TextView
android:id="#+id/address"
android:padding="3dip"
android:gravity="left"
android:text="Address"
/>
<TextView
android:id="#+id/age"
android:padding="3dip"
android:gravity="left"
android:text="Age"
/>
</TableRow>
</TableLayout>
Step #1: Pull that row out into a separate layout file
Step #2: Inflate that layout file whenever you need a row
Step #3: Call addView() on the TableLayout to add the inflated row

Categories

Resources