Android Error:必须提供layout_width属性

时间:2021-08-12 20:56:59

Sorry because my title similar with many others on * but none of those solution meet with my problem.

抱歉,因为我的标题与*上的许多其他的标题相似,但是没有一个解决方案能解决我的问题。

I'm designing a layout use Relative Layout. After design in code view, when I change to graphic view, Eclipse notices :

我正在设计一个相对布局。在代码视图中进行设计后,当我更改为图形视图时,Eclipse注意到:

You must supply a layout_width attribute.

您必须提供一个layout_width属性。

You must supply a layout_height attribute.

您必须提供一个layout_height属性。

. When I run this program, will notice error in LogCat

。当我运行这个程序时,会注意到LogCat中的错误

Caused by: java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.

引起的:. lang。RuntimeException:二进制XML文件第2行:必须提供layout_width属性。

Here is my XML file:

下面是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/contact_name"
        android:text="Sam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/contact_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:text="39256493"
        android:layout_below="@+id/contact_name"
        android:layout_alignLeft="@+id/contact_name"
        android:layout_marginTop="5dp"/>        

</RelativeLayout>

4 个解决方案

#1


13  

Your xmlns line should read xmlns:android="http://schemas.android.com/apk/res/android". That'll probably fix your issue.

您的xmlns行应该读取xmlns:android=“http://schemas.android.com/apk/res/android”。这可能会解决你的问题。

EDIT:

编辑:

Quoting Google in http://developer.android.com/guide/topics/manifest/manifest-element.html:

引用谷歌在http://developer.android.com/guide/topics/manifest/manifest-element.html:

xmlns:android Defines the Android namespace.

xmlns:android定义了android命名空间。

This attribute should always be set to "http://schemas.android.com/apk/res/android".

这个属性应该始终设置为“http://schemas.android.com/apk/res/android”。

When you are using a XML file like the layout files you are using, the attributes you can use within that same file are defined by a schema.

当您使用XML文件(如您正在使用的布局文件)时,您可以在同一文件中使用的属性由模式定义。

xmlns is short for "XML namespace". You're defining inside your XML a namespace of keywords "android:", and that's why you need to declare all your attributes with "android:" in the beginning, such as android:layout_height or android:layout_width.

xmlns是“XML命名空间”的缩写。你在XML中定义了一个关键词“android:”的名称空间,这就是为什么你需要在开始时使用android:layout_height或android:layout_width来声明你的所有属性。

The namespace should be pointing to a valid schema, which should be pointing to a URLcontaining that exact schema. If the URL doesn't point to a valid schema, your XML attributes won't be recognized, which was the problem you were having.

名称空间应该指向一个有效的模式,该模式应该指向一个包含该模式的url。如果URL没有指向有效的模式,您的XML属性将无法识别,这就是您遇到的问题。

I hope you could understand my explanation.

我希望你能理解我的解释。

If you want to read more about XML namespaces and schemas, I can point you straight to W3Schools and Wikipedia: http://www.w3schools.com/xml/xml_namespaces.asp, http://en.wikipedia.org/wiki/XML_schema.

如果您想了解更多关于XML名称空间和模式的内容,我可以直接向您指出W3Schools和Wikipedia: http://www.w3schools.com/xml/xml_namespaces.asp, http://en.wikipedia.org/wiki/XML_schema。

#2


2  

It might be irrelevant but namespace tag attracted my attention, as the usual one is:

它可能不相关,但名称空间标记吸引了我的注意,一如往常:

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

#3


1  

You should change line : xmlns:android="http://schemas.android.com to line : xmlns:android="http://schemas.android.com/apk/res/android

您应该更改行:xmlns:android="http://schemas.android.com to line: xmlns:android="http://schemas.android.com/apk/res/android。

I have met this error and It;s painful. But I cannot explain why.

我遇到了这个错误,很痛苦。但我无法解释原因。

Hope it works for your :)

希望它对你的:)

#4


0  

Could you try doing projects > clean. If that doesn't work try changing your relative layout's arguments with match_parent.

你能不能试着把项目清理一下。如果这不起作用,请尝试与match_parent更改相对布局的参数。

#1


13  

Your xmlns line should read xmlns:android="http://schemas.android.com/apk/res/android". That'll probably fix your issue.

您的xmlns行应该读取xmlns:android=“http://schemas.android.com/apk/res/android”。这可能会解决你的问题。

EDIT:

编辑:

Quoting Google in http://developer.android.com/guide/topics/manifest/manifest-element.html:

引用谷歌在http://developer.android.com/guide/topics/manifest/manifest-element.html:

xmlns:android Defines the Android namespace.

xmlns:android定义了android命名空间。

This attribute should always be set to "http://schemas.android.com/apk/res/android".

这个属性应该始终设置为“http://schemas.android.com/apk/res/android”。

When you are using a XML file like the layout files you are using, the attributes you can use within that same file are defined by a schema.

当您使用XML文件(如您正在使用的布局文件)时,您可以在同一文件中使用的属性由模式定义。

xmlns is short for "XML namespace". You're defining inside your XML a namespace of keywords "android:", and that's why you need to declare all your attributes with "android:" in the beginning, such as android:layout_height or android:layout_width.

xmlns是“XML命名空间”的缩写。你在XML中定义了一个关键词“android:”的名称空间,这就是为什么你需要在开始时使用android:layout_height或android:layout_width来声明你的所有属性。

The namespace should be pointing to a valid schema, which should be pointing to a URLcontaining that exact schema. If the URL doesn't point to a valid schema, your XML attributes won't be recognized, which was the problem you were having.

名称空间应该指向一个有效的模式,该模式应该指向一个包含该模式的url。如果URL没有指向有效的模式,您的XML属性将无法识别,这就是您遇到的问题。

I hope you could understand my explanation.

我希望你能理解我的解释。

If you want to read more about XML namespaces and schemas, I can point you straight to W3Schools and Wikipedia: http://www.w3schools.com/xml/xml_namespaces.asp, http://en.wikipedia.org/wiki/XML_schema.

如果您想了解更多关于XML名称空间和模式的内容,我可以直接向您指出W3Schools和Wikipedia: http://www.w3schools.com/xml/xml_namespaces.asp, http://en.wikipedia.org/wiki/XML_schema。

#2


2  

It might be irrelevant but namespace tag attracted my attention, as the usual one is:

它可能不相关,但名称空间标记吸引了我的注意,一如往常:

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

#3


1  

You should change line : xmlns:android="http://schemas.android.com to line : xmlns:android="http://schemas.android.com/apk/res/android

您应该更改行:xmlns:android="http://schemas.android.com to line: xmlns:android="http://schemas.android.com/apk/res/android。

I have met this error and It;s painful. But I cannot explain why.

我遇到了这个错误,很痛苦。但我无法解释原因。

Hope it works for your :)

希望它对你的:)

#4


0  

Could you try doing projects > clean. If that doesn't work try changing your relative layout's arguments with match_parent.

你能不能试着把项目清理一下。如果这不起作用,请尝试与match_parent更改相对布局的参数。