frequent issues arising in android view, Error parsing XML: unbound prefix
Asked Answered
T

16

309

I have frequent problem in android view, Error parsing XML: unbound prefix on Line 2.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:id="@+id/myScrollLayout" 
android:layout_width="fill_parent"  android:layout_height="wrap_content">
    <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" 
    android:text="Family" android:id="@+id/Family" 
    android:textSize="16px" android:padding="5px" 
    android:textStyle="bold" android:gravity="center_horizontal">
    </TextView>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="vertical" android:scrollbars="vertical">
        <LinearLayout android:orientation="vertical" android:id="@+id/myMainLayout" 
        android:layout_width="fill_parent"  android:layout_height="wrap_content">
        </LinearLayout>
    </ScrollView>

</LinearLayout>
Trestlework answered 8/2, 2010 at 11:29 Comment(2)
Check all the answers posted for this; they are all valid. The issue is XML namespaces and several ways it can go horribly wrong.Telegony
A quick note, if you forget the supress ALL comment in your very first line, you won't even notice that something is wrong with your layout(like this: <?xml version="1.0" encoding="utf-8"?><!--suppress ALL --> )Stealth
T
573

A couple of reasons that this can happen:

1) You see this error with an incorrect namespace, or a typo in the attribute. Like 'xmlns' is wrong, it should be xmlns:android

2) First node needs to contain: xmlns:android="http://schemas.android.com/apk/res/android"

3) If you are integrating AdMob, check custom parameters like ads:adSize, you need

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

4) If you are using LinearLayout you might have to define tools:

xmlns:tools="http://schemas.android.com/tools"

Trestlework answered 8/2, 2010 at 12:3 Comment(3)
What do you mean by "the first node"? In the original question is that the TextView?Gunilla
...and in the new Google Play AdMob it's :xmlns:ads="schemas.android.com/apk/res-auto"Anthozoan
Thanks a lot, I solved my issue in case 3 by adding this line xmlns:ads="schemas.android.com/apk/lib/com.google.ads" +1 for the answerDonaugh
P
101

I'm going to add a separate answer just because I don't see it here. It's not 100% what Pentium10 asked for, but I ended up here by searching for Error parsing XML: unbound prefix

Turns out I was using custom parameters for AdMob ads like ads:adSize, but I hadn't added

    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

to the layout. Once I added it it worked great.

Prototype answered 16/12, 2012 at 8:18 Comment(0)
C
64

I had this same problem.

Make sure that the prefix (android:[whatever]) is spelled correctly and written correctly. In the case of the line xmlns:android="http://schemas.android.com/apk/res/android" make sure that you have the full prefix xmlns:android and that it is spelled correctly. Same with any other prefixes - make sure they are spelled correctly and have android:[name]. This is what solved my problem.

Chickadee answered 20/9, 2010 at 6:54 Comment(2)
Yes, in my case the error was caused by androi:src="@drawable/half" - which was clearly wrong!Millesimal
Yes, I had the same problem. I was missing the "xmln:" part as well.Swetlana
J
33

As you mention, you need to specify the right namespace. You also see this error with an incorrect namespace.

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

will not work.

Change:

xmlns="http://schemas.android.com/apk/res/android"

to

xmlns:android="http://schemas.android.com/apk/res/android"

The error message is referring to everything that starts "android:" as the XML does not know what the "android:" namespace is.

xmlns:android defines it.

Jakoba answered 8/2, 2010 at 12:46 Comment(0)
P
23

This error may occurs in the case you use un-defined prefix such as:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TabHost
    XYZ:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


</TabHost>

Android compiler does not know what is XYZ since it was not defined yet.

In your case, you should add below define to root node of the xml file.

xmlns:android="http://schemas.android.com/apk/res/android"

Pygidium answered 8/12, 2011 at 7:42 Comment(2)
This one did it for me. It was a lame mistype : Instead of android:layout_weight I had typed anrdoid:layout_weigth.Charteris
Nguyen, how to define XYZ? ThanksDeflected
L
11

unbound prefix error for ViewPager Indicator:

Along with the following header tags in your parentLayout:

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

Also add:

xmlns:app="http://schemas.android.com/apk/res-auto"

This did the trick for me.

Lawton answered 10/2, 2014 at 12:28 Comment(0)
L
9

For me, I got the "unbound prefix" error on first line here, although I had misspelled android on the fourth line.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
anrdoid:fillViewport="true"
>
Lailaibach answered 14/8, 2012 at 20:2 Comment(0)
T
7

I had the same problem, and found that the solution was to add the android:tools to the first node. In my case it is a LineraLayout:

    <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="vertical">
Tanana answered 27/9, 2012 at 14:11 Comment(1)
Yes, if you reference tools, but don't add the namespace for tools, you'll get this as well.Wetzel
C
3

I'll throw in a little more for the newbies and for folks, like myself, that don't understand XML.

The answers above a pretty good, but the general answer is that you need a namespace for any namespace used in the config.xml file.

Translation: Any XML tag name that has is a tag with a namespace where blah is the namespace and fubar is the XML tag. The namespace lets you use many different tools to interpret the XML with their own tag names. For example, Intel XDK uses the namespace intelxdk and android uses android. Thus you need the following namespaces or the build throws up blood (i.e. Error parsing XML: unbound prefix) which is translated to: You used a namespace, but did not define it.

  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:intelxdk="http://xdk.intel.com/ns/v1"
Catenary answered 24/12, 2014 at 6:41 Comment(0)
P
3

OK, there are lots of solutions here but non actually explain the root cause of the problem so here we go:

when you see an attribute like android:layout_width="match_parent" the android part is the prefix, the format for an attribute here is PREFIX:NAME="VALUE". in XML, namespaces and prefixes are ways to avoid naming conflicts for example we can have two distinct attributes with same name but different prefixes like: a:a="val" and b:a="val".

to use prefixes like android or app or any other your should define a namespace using xmlns attribute.

so if you have this issue just find prefixes that do not have a namespace defined, if you have tools:... you should add tools namespace as some answeres suggested, if you have app:... attribute you should add xmlns:app="http://schemas.android.com/apk/res-auto" to the root element

Further reading:

XML Namespaces simple explanation

XML Namespaces in W3

Pelerine answered 19/9, 2018 at 21:25 Comment(0)
B
2

This error usually occurs if you have not included the xmlns:mm properly, it occurs usually in the first line of code.

for me it was..

xmlns:mm="http://millennialmedia.com/android/schema"

that i missed in first line of the code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mm="http://millennialmedia.com/android/schema"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:background="@android:color/transparent" >
Birchfield answered 14/2, 2014 at 8:1 Comment(0)
I
2

You just need to add proper name space in your root tag . xmlns:android="http://schemas.android.com/apk/res/android" Android elemets are declared in this name space.Its same as importing class or package.

Imes answered 30/6, 2016 at 7:0 Comment(0)
P
1

In my case the error was not caused by any of the above xml namespace issues. Instead it was the location of the android:id attribute - it needed to be the first item in the particular element's declaration.

So this:

<TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:id="@+id/bottomtext" 
      android:singleLine="true" />

... needed to read like this:

<TextView android:id="@+id/bottomtext" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:singleLine="true" />
Preachy answered 15/7, 2012 at 3:23 Comment(0)
T
1

Beside all this, There is also a scenario where this error occures-

When you or your library project define custom attribute int attr.xml, And you use these attributes in your layout file without defining namespace .

Generally we use this namespace definition in header of our layout file.

xmlns:android="http://schemas.android.com/apk/res/android"

Then make sure all attributes in your file shoud start with

android:ATTRIBUTE-NAME

You need to identfy if some of your attirbute is not starting with something other than android:ATTRIBUTE-NAME like

temp:ATTRIBUTE-NAME

In this case you have this "temp" also as namespace, generally by including-

xmlns:temp="http://schemas.android.com/apk/res-auto"
Theorbo answered 10/12, 2014 at 5:54 Comment(0)
A
0

It usually happens to me when I misspell android - I just type andorid or alike, and it's not obvious at first sight especially after many hours of programming, so I just do a search for "android" one by one and see if search skips one tag - if it does then I have a close look and I see where was typo.

Ashjian answered 12/12, 2014 at 6:45 Comment(0)
T
0

I got this error in Xamarin when I was using

  <android.support.v7.widget.CardView  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    card_view:cardElevation="4dp"  
    card_view:cardCornerRadius="5dp"  
    card_view:cardUseCompatPadding="true">  
  </android.support.v7.widget.CardView>

in a layout file without installing the nuget package for android.support.v7.widget.CardView

Installing the applicable nuget package fixed the issue. Hope it helps, I didn't see this answer anywhere in the list

Transitive answered 22/2, 2020 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.