android view中出现的常见问题,Error parsing XML: unbound prefix
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2221221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
frequent issues arising in android view, Error parsing XML: unbound prefix
提问by Pentium10
I have frequent problem in android view, Error parsing XML: unbound prefix on Line 2
.
我在 android 视图中经常遇到问题,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>
回答by Pentium10
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
1) 您看到此错误,名称空间不正确,或属性中存在拼写错误。就像'xmlns'是错误的,应该是xmlns:android
2) First node needs to contain:
xmlns:android="http://schemas.android.com/apk/res/android"
2)第一个节点需要包含:
xmlns:android="http://schemas.android.com/apk/res/android"
3) If you are integrating AdMob, check custom parameters like ads:adSize
, you need
3) 如果您要集成 AdMob,请检查自定义参数,例如ads:adSize
,您需要
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
4) If you are using LinearLayout
you might have to define tools:
4)如果您正在使用,LinearLayout
您可能需要定义工具:
xmlns:tools="http://schemas.android.com/tools"
xmlns:tools="http://schemas.android.com/tools"
回答by MalcolmOcean
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
我将添加一个单独的答案,因为我在这里没有看到它。这不是 Pentium10 要求的 100%,但我最终在这里搜索Error parsing XML: unbound prefix
Turns out I was using custom parameters for AdMob ads like ads:adSize
, but I hadn't added
原来我使用的是 AdMob 广告的自定义参数,例如ads:adSize
,但我没有添加
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
to the layout. Once I added it it worked great.
到布局。一旦我添加它,它就很好用。
回答by Daniel
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.
确保前缀 (android:[whatever]) 拼写正确且书写正确。对于该行xmlns:android="http://schemas.android.com/apk/res/android"
,请确保您具有完整的前缀xmlns:android
并且拼写正确。与任何其他前缀相同 - 确保它们拼写正确并且有android:[name]
. 这就是解决我的问题的原因。
回答by VonC
As you mention, you need to specify the right namespace. You also see this error with an incorrect namespace.
正如您所提到的,您需要指定正确的 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.
错误消息是指以“android:”开头的所有内容,因为 XML 不知道“
android:
”命名空间是什么。
xmlns:android
定义它。
回答by Nguyen Minh Binh
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.
Android 编译器不知道什么是 XYZ,因为它还没有定义。
In your case, you should add below define to root node of the xml file.
在您的情况下,您应该将以下定义添加到 xml 文件的根节点。
xmlns:android="http://schemas.android.com/apk/res/android"
回答by amalBit
unbound prefix error for ViewPager Indicator:
ViewPager 指标的未绑定前缀错误:
Along with the following header tags in your parentLayout:
连同 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.
这对我有用。
回答by flobacca
For me, I got the "unbound prefix" error on first line here, although I had misspelled android on the fourth line.
对我来说,虽然我在第四行拼错了 android,但我在第一行收到了“未绑定前缀”错误。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
anrdoid:fillViewport="true"
>
回答by Sebastian Breit
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:
我遇到了同样的问题,发现解决方案是将 android:tools 添加到第一个节点。就我而言,它是一个 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">
回答by turbogeek
I'll throw in a little more for the newbies and for folks, like myself, that don't understand XML.
对于新手和像我这样不懂 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.
上面的答案相当不错,但一般的答案是,您需要为 config.xml 文件中使用的任何命名空间指定一个命名空间。
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.
翻译:任何具有命名空间的 XML 标记名称,其中 blah 是命名空间,fubar 是 XML 标记。命名空间允许您使用许多不同的工具来解释具有自己的标记名称的 XML。例如,Intel XDK 使用命名空间 intelxdk,而 android 使用 android。因此,您需要以下命名空间,否则构建会吐血(即解析 XML 时出错:未绑定前缀),它被转换为:您使用了命名空间,但没有定义它。
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:intelxdk="http://xdk.intel.com/ns/v1"
回答by mohas
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"
.
当你看到这样一个属性android:layout_width="match_parent"
的android
部分是前缀,属性的格式在这里PREFIX:NAME="VALUE"
。在 XML 中,命名空间和前缀是避免命名冲突的方法,例如,我们可以有两个不同的属性,名称相同但前缀不同,例如:a:a="val"
和b:a="val"
。
to use prefixes like android
or app
or any other your should define a namespace using xmlns
attribute.
要使用像android
或app
或任何其他前缀,您应该使用xmlns
属性定义命名空间。
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
因此,如果您遇到此问题,只需找到未定义名称空间的前缀,如果您有,tools:...
您应该添加工具名称空间作为某些答案的建议,如果您有app:...
属性,您应该添加xmlns:app="http://schemas.android.com/apk/res-auto"
到根元素
Further reading:
进一步阅读: