Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Error :You must supply layout_width attribute

Sorry because my title similar with many others on StackOverFlow 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 :

You must supply a layout_width attribute.

You must supply a layout_height attribute.

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

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

Here is my XML file:

<?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>
like image 229
hqt Avatar asked Mar 02 '12 14:03

hqt


2 Answers

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

EDIT:

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

xmlns:android Defines the Android namespace.

This attribute should always be set to "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.

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.

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.

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.

like image 192
jcxavier Avatar answered Oct 13 '22 20:10

jcxavier


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

xmlns:android="http://schemas.android.com/apk/res/android"
like image 22
Orkun Ozen Avatar answered Oct 13 '22 20:10

Orkun Ozen