Android error: you must specify the layout_width attribute

Sorry, because my title is similar to many others on StackOverFlow , but none of these solutions answer my problem.

I am designing using a Relative Layout layout. After designing in the code view, when I move on to the graphical view, Eclipse notes:

You must specify the layout_width attribute.

You must specify the layout_height attribute.

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

Raised: java.lang.RuntimeException: binary line of XML file # 2: you must provide the 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> 
+6
source share
4 answers

Your xmlns line should read xmlns:android="http://schemas.android.com/apk/res/android" . This will probably fix your problem.

EDIT:

Quoting Google at 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 use an XML file, such as layout files that you use, attributes that you can use in the same file are defined by the schema.

xmlns is not suitable for the "XML namespace". You define inside your XML the keyword namespace "android:", and so you need to declare all your attributes with "android:" at the beginning, for example android:layout_height or android:layout_width .

The namespace must point to a valid scheme, which must point to a URL containing this exact scheme. If the URL does not point to a valid schema, your XML attributes will not be recognized, which was the problem you encountered.

Hope you could understand my explanation.

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

+13
source

It may not matter, but the namespace tag caught my attention, as usual :

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

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

I met this error, and it is painful. But I can’t explain why.

Hope this works for yours :)

+1
source

Could you try running projects> clean. If this does not work, try changing your match arguments with match_parent.

0
source

Source: https://habr.com/ru/post/909832/


All Articles