可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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>
回答1:
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\"
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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\"
回答6:
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.
回答7:
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\"
>
回答8:
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\">
回答9:
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\"
回答10:
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\" >
回答11:
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\" />
回答12:
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\"
回答13:
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.
回答14:
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
回答15:
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.